JSON(JavaScript Object Notation)
?? ?1.一種輕量級的數(shù)據(jù)交換格式
?? ?2.通常用于在客戶端和服務器之間傳遞數(shù)據(jù)
?? ?3.jQuery的所有參數(shù)都是以JSON格式出現(xiàn)
?? ?4.在Struts 2中通過插件的方式實現(xiàn)
優(yōu)勢(較XML):
?? ?1.輕量級交換語言
?? ?2.結(jié)構(gòu)簡單
?? ?3.易于解析
?? ?
JSON對象:
?? ?語法:
?? ??? ?var JSON對象 = {key:value,key:value,……};
?? ?范例:{"id":4,"name":"梅西","pwd":"6666"}
【注意:其中的key值必須是字符串】
?? ?
JSON數(shù)組:
?? ?語法:
?? ??? ?var JSON數(shù)組 = [value,value,……];
?? ?范例:
?? ??? ?var countryArray = ["中國","美國","俄羅斯"];
?? ??? ?var personArray = [
?? ??? ??? ??? ??? ??? ??? ?{"name":"張三","age":30},
?? ??? ??? ??? ??? ??? ??? ?{"name":"李四","age":40}
?? ??? ??? ??? ??? ??? ?? ];
?? ??? ??? ??? ??? ??? ? ?
步驟:
?? ?1.導入struts2-json-plugin-xxx.jar
?? ?2.在struts.xml中定義package并繼承json-default
?? ?3.指定<result>的type屬性指定為"json"
================================Result===============================?? ?
一、JSON類型的Result:
?? ?1.參數(shù)說明:
?? ??? ?參數(shù)?? ??? ??? ??? ??? ?作用?? ??? ??? ??? ??? ??? ??? ??? 默認值?? ??? ??? ??? ??? ??? ??? ??? ??? 適用場景
?? ??? ?root 指定要序列化的根對象?? ??? ??? ?? 當前Actin中所有有返回值的getter方法的值?? ?用于指定不需要序列化key值的數(shù)據(jù)
?? ??? ?includeProperties 指定根對象中要序列化的屬性?? ??? ??? ?當前根對象中的所有屬性?? ?? 用于需要序列化的屬性較少的情況
?? ??? ?excludeProperties?? ??? 指定根對象中要排除的屬性?? ??? ??? ? null?? ??? ??? ??? 用于需要排除序列化的屬性較少的情況
?? ??? ?excludeNullProperties?? ?指定根對象中是否序列化值為空的屬性?? ?false?? ??? ??? ?? 用于需要過濾空值的情況
?? ?2.步驟:
?? ??? ?a.添加第三方插件:struts2-json-plugin-xxx.jar
?? ??? ?b.在struts.xml中指定package繼承json-default
?? ??? ?c.指定<result>的type屬性為“json”
?? ??? ?d.根據(jù)業(yè)務需求合理指定param參數(shù)
?? ?
?? ?3.作用:
?? ??? ?a.返回JSON格式數(shù)據(jù),配合jQuery實現(xiàn)Ajax
?? ??? ?b.param參數(shù)
?? ??? ?c.root、 includeProperties、 excludeProperties、 excludeNullProperties
范例:登錄表單
1.User實體類
1 package com.Elastic.StrutsDemo6.ivy.entity;
2 spublic
class User {
3 private String loginName;
4 private String loginPass;
5
6 public String getLoginName() {
7 return loginName;
8 }
9 public void setLoginName(String loginName) {
10 this.loginName =
loginName;
11 }
12 public String getLoginPass() {
13 return loginPass;
14 }
15 public void setLoginPass(String loginPass) {
16 this.loginPass =
loginPass;
17 }
18
19 }
2.UserAction類
1 package com.Elastic.StrutsDemo6.ivy.action;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import com.Elastic.StrutsDemo6.ivy.entity.User;
8 import com.opensymphony.xwork2.ActionSupport;
9
10 public class UserAction
extends ActionSupport {
11 private User user;
12
13 public User getUser() {
14 return user;
15 }
16
17 public void setUser(User user) {
18 this.user =
user;
19 }
20
21 private Map<String, Object> jsonResult =
new HashMap<String, Object>
();
22
23 public Map<String, Object>
getJsonResult() {
24 return jsonResult;
25 }
26
27 public void setJsonResult(Map<String, Object>
jsonResult) {
28 this.jsonResult =
jsonResult;
29 }
30
31 public String login() {
32
33 //登陸成功以后,額外添加的信息
34 /*jsonResult.put("success", true);
35 jsonResult.put("data", user);
36 jsonResult.put("msg", "登陸成功");
37 jsonResult.put("loginTime", new Date());*/
38
39 if ("admin".equals(user.getLoginName().trim()) && "123456"
.equals(user.getLoginPass().trim())) {
40 jsonResult.put("success",
true);
41 jsonResult.put("data"
, user);
42 jsonResult.put("msg", "登陸成功"
);
43 jsonResult.put("loginTime",
new Date());
44 }
else {
45 jsonResult.put("success",
false);
46 jsonResult.put("msg", "登陸失敗"
);
47 jsonResult.put("loginTime",
new Date());
48 }
49 return SUCCESS;
50 }
51 }
3.struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
3 <struts>
4 <!-- 使用JSON配置 -->
5 <package name="jsonDefault" namespace="/" extends="json-default">
6 <action name="login" class="com.Elastic.StrutsDemo6.ivy.action.UserAction" method="login">
7
8 <!-- 配置JSON類型的result -->
9 <!--type屬性指定為"json"",將返回序列化的JSON格式數(shù)據(jù)-->
10 <result type="json">
11 <!-- <param name="root">user</param> -->
12 <param name="root">jsonResult
</param>
13 </result>
14 </action>
15
16 </package>
17 </struts>
4.web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
3 <display-name>StrutsDemo6_ivy
</display-name>
4 <welcome-file-list>
5 <welcome-file>index.html
</welcome-file>
6 <welcome-file>index.htm
</welcome-file>
7 <welcome-file>index.jsp
</welcome-file>
8 <welcome-file>default.html
</welcome-file>
9 <welcome-file>default.htm
</welcome-file>
10 <welcome-file>default.jsp
</welcome-file>
11 </welcome-file-list>
12
13 <filter>
14 <filter-name>Struts2
</filter-name>
15 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
16 </filter>
17 <filter-mapping>
18 <filter-name>Struts2
</filter-name>
19 <url-pattern>/*
</url-pattern>
20 </filter-mapping>
21
22 </web-app>
5.valid.js
1 /**
2 * rules: 對象
3 */
4
5 var rules =
{
6 loginName : {
7 rule : {
8 required :
true,
9 length : [6,12
]
10 },
11 msg : {
12 required : '用戶名不能為空'
,
13 length : '用戶名的長度必須在6-12位之間'
14 }
15 },
16
17 loginPass : {
18 rule : {
19 length : [6,12
]
20 },
21 msg : {
22 length : '密碼的長度必須在6-12位之間'
23 }
24 }
25 };
26
27 function validForm(form,rules) {
28 for (
var p
in rules) {
29 form.find('#' + p).on('valid',rules[p],
function(event){
30 //使用event.data, msg內(nèi)容才會不同!!!
31 var inputRules =
event.data.rule;
32
33 for (
var r
in inputRules) {
34 var value = $(
this).val();
35 if (r === 'required' && inputRules[r] ===
true) {
36 if (value === ''
) {
37 $(
this).data('success',
false);
38
39 $(
this).closest('div'
).next().html(event.data.msg[r]);
40
41 }
else {
42 $(
this).data('success',
true);
43 $(
this).closest('div').next().html(''
);
44 }
45 }
46
47 //驗證先后順序
48 /*if ($(this).data('success') === false) {
49 return;
50 }*/
51
52 if (r === 'length' &&
inputRules[r]) {
53 if (value.length > inputRules[r][1] || value.length < inputRules[r][0
]) {
54 $(
this).data('success',
false);
55
56 $(
this).closest('div'
).next().html(event.data.msg[r]);
57
58 }
else {
59 $(
this).data('success',
true);
60 $(
this).closest('div').next().html(''
);
61 }
62 }
63 }
64 });
65 }
66
67 //事件綁定一次
68 function valid() {
69 //trigger:觸發(fā)valid事件
70 form.find(':input').trigger('valid'
);
71 var success =
true;
72
73 form.find(':input').each(
function() {
74 if ($(
this).data('success') ===
false) {
75 success =
false;
76 return;
77 }
78 });
79 return success;
80 }
81
82 return valid;
83 }
6.login.jsp
1 <%--引入JSP頁面PAGE指令 --%>
2 <%@ page language="java" contentType="text/html; charset=UTF-8"
3 pageEncoding="UTF-8"%>
4 <%-- 引入JSTL標簽指令 --%>
5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
6 <!DOCTYPE html>
7 <html language="zh-CN">
8 <head>
9 <meta charset="utf-8">
10 <!-- 設置瀏覽器渲染的引擎 -->
11 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12 <!-- 設置支持移動設備 -->
13 <meta name="viewport" content="width=device-width, initial-scale=1">
14 <title>網(wǎng)頁標題
</title>
15 <!-- 引入bootstrap的樣式 -->
16 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/bootstrap.min.css">
17 </head>
18 <body>
19 <div class="container">
20
21 <div class="panel panel-primary">
22 <div class="panel-heading">登陸
</div>
23 <div class="panel-body">
24 <form class="form-horizontal" action="login" method="post" data-role="login">
25 <div class="form-group">
26 <label class="col-md-3 control-label" for="loginName">用戶名:
</label>
27 <div class="col-md-6">
28 <input class="form-control" id="loginName" name="user.loginName" type="text" autocomplete="off" />
29 </div>
30 <div class="col-md-3">
31
32 </div>
33 </div>
34 <div class="form-group">
35 <label class="col-md-3 control-label" for="loginPass">密碼:
</label>
36 <div class="col-md-6">
37 <input class="form-control" id="loginPass" name="user.loginPass" type="password" />
38 </div>
39 <div class="col-md-3">
40
41 </div>
42 </div>
43 <div class="form-group">
44 <input class="btn btn-primary btn-block" data-role="login" type="button" value="登陸" />
45 </div>
46 </form>
47 </div>
48 </div>
49 </div>
50
51 <!-- 引入JS的樣式 -->
52 <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-2.2.4.js"></script>
53 <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script>
54
55 <script type="text/javascript" src="<%=request.getContextPath()%>/js/valid.js"></script>
56
57 <script type="text/javascript">
58 //JSON對象
59 //測試
60 /* var person = {
61 "name":"李四",
62 "age":20
63 };
64
65 alert(person.age); */
66
67 $(function() {
68 //方法二
69 //給表單元素綁定一個自定義的驗證事件
70 /* $('#loginName').on('valid', function() {
71 var name = $(this).val();
72 if ('' == name) {
73 $(this).closest('div').next().html('用戶名不能為空!');
74 $(this).data('success', false);
75 } else {
76 $(this).closest('div').next().html('');
77 $(this).data('success', true);
78 }
79
80 });
81 $('#loginPass').on('valid', function() {
82 var name = $(this).val();
83 if ('' == name) {
84 $(this).closest('div').next().html('密碼不能為空!');
85 $(this).data('success', false);
86 } else {
87 $(this).closest('div').next().html('');
88 $(this).data('success', true);
89 }
90
91 }); */
92
93 var valid = validForm($('form'), rules);
94
95 $('[data-role="login"]').click(function() {
96 //獲取表單
97 var $form = $(this).closest('form');
98
99 $.ajax({
100 url : "login",
101 type : "post",
102 data : $form.serialize(),
103 //data : $(this).closest('form').serialize(),
104 beforeSend : function() {
105 /* var success = true;
106
107 //trigger:觸發(fā)valid事件
108 $form.find(':input').trigger('valid');
109 $form.find(':input').each(function() {
110 if ($(this).data('success') === false) {
111 success = false;
112 return;
113 }
114 });
115 return success;*/
116
117 //方法三
118 //返回方法
119 return valid();
120
121 //方法一:
122 /* var name = $('#loginName').val();
123 if ('' == name) {
124 $('#loginName').closest('div').next().html('用戶名不能為空!');
125 return false;
126 } else {
127 $('#loginName').closest('div').next().html('');
128 }
129
130 var pass = $('#loginPass').val();
131 if ('' == pass) {
132 $('#loginPass').closest('div').next().html('密碼不能為空!');
133 return false;
134 } else {
135 $('#loginPass').closest('div').next().html('');
136 } */
137 },
138 success : function(result) {
139 //返回result(對象)
140 //alert(result);
141 alert(result.msg);
142 if (result.success) {
143 //window.location = 'index.jsp';
144 }
145 }
146
147 });
148 });
149 });
150 </script>
151 </body>
152 </html>
二、Stream類型的Result?? ?
?? ?1.步驟:
?? ??? ?a.定義一個InputStream類型的成員變量
?? ??? ?b.給該變量添加getter和setter方法
?? ??? ?c.將要發(fā)送到客戶端的數(shù)據(jù)賦值給該變量
?? ??? ?d.根據(jù)業(yè)務需求合理指定param參數(shù)
?? ??? ?
?? ?2.作用:
?? ??? ?a.返回二進制數(shù)據(jù),配合jQuery實現(xiàn)Ajax,或者實現(xiàn)驗證碼
?? ?
?? ?3.param參數(shù)
?? ??? ?a.contentType、inputStream
范例:驗證碼--在上一個范例的基礎上修改
1.CodeAction類
1 package com.Elastic.StrutsDemo6.ivy.action;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.FontMetrics;
6 import java.awt.Graphics2D;
7 import java.awt.image.BufferedImage;
8 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.Date;
13
14 import javax.imageio.ImageIO;
15
16 import com.opensymphony.xwork2.ActionContext;
17 import com.opensymphony.xwork2.ActionSupport;
18
19 public class CodeAction
extends ActionSupport {
20 private InputStream inputStream;
21
22 public InputStream getInputStream()
throws IOException {
23 //1.隨機生成4位的驗證碼
24 String[] strs = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"
,
25 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v"
,
26 "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7"
,
27 "8", "9"
};
28 String code = ""
;
29 for (
int i = 0; i < 4; i++
) {
30 String temp = strs[(
int)(Math.random() *
strs.length)];
31
32 //隨機生成大寫字母
33 temp = (
int)(Math.random() * 2) == 0 ?
temp : temp.toUpperCase();
34 code +=
temp;
35 }
36
37 //2.生成驗證碼圖片
38 BufferedImage codeImg =
new BufferedImage(100, 25
, BufferedImage.TYPE_INT_RGB);
39 //得到圖片的繪制對象
40 Graphics2D g2d =
codeImg.createGraphics();
41 //設置填充顏色并填充
42 g2d.setColor(
new Color(240, 240, 240
));
43 g2d.fillRect(0, 0
, codeImg.getWidth(), codeImg.getHeight());
44 //繪制驗證碼到圖片上
45 Font font =
new Font("微軟雅黑", Font.PLAIN, 20
);
46 g2d.setFont(font);
47 //獲取字體寬度
48 FontMetrics fm =
g2d.getFontMetrics();
49 //設置左右空白和字體間隔
50 float start = 15
;
51 float space = (
float)(codeImg.getWidth() - 2 * start - fm.getStringBounds(code, g2d).getWidth()) / (code.length() - 1
);
52 //隨機生成每個字符的樣式
53 for (
int i = 0; i < code.length(); i++
) {
54 g2d.setColor(getRandomColor());
55 String temp = code.charAt(i) + ""
;
56 g2d.drawString(temp, start, font.getSize());
57 start += fm.getStringBounds(temp, g2d).getWidth() +
space;
58 }
59
60 //繪制干擾項
61 //隨機生成點
62 for (
int i = 0; i < 100; i++
) {
63 g2d.setColor(getRandomColor());
64 int x = (
int)(Math.random() *
codeImg.getWidth());
65 int y = (
int)(Math.random() *
codeImg.getHeight());
66 g2d.drawLine(x, y, x, y);
67 }
68 //隨機生成線
69 for (
int i = 0; i < 10; i++
) {
70 g2d.setColor(getRandomColor());
71 int x = (
int)(Math.random() *
codeImg.getWidth());
72 int y = (
int)(Math.random() *
codeImg.getHeight());
73 int x1 = (
int)(Math.random() *
codeImg.getWidth());
74 int y1 = (
int)(Math.random() *
codeImg.getHeight());
75 g2d.drawLine(x, y, x1, y1);
76 }
77
78 ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();
79 ImageIO.write(codeImg, "png"
, byteArrayOutputStream);
80 ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
81
82 //把驗證碼保存到session中
83 ActionContext.getContext().getSession().put("code"
, code);
84 //設置驗證碼有效時間
85 ActionContext.getContext().getSession().put("codeMaxTime",
new Date().getTime() + 10 * 1000
);
86
87 return byteArrayInputStream;
88 }
89
90 public Color getRandomColor() {
91 int r = (
int)(Math.random() * 256
);
92 int g = (
int)(Math.random() * 256
);
93 int b = (
int)(Math.random() * 256
);
94 return new Color(r, g, b);
95 }
96
97 }
2.struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
3 <struts>
4
5 <!-- 使用JSON配置 -->
6 <package name="jsonDefault" namespace="/" extends="json-default">
7 <action name="login" class="com.Elastic.StrutsDemo6.ivy.action.UserAction" method="login">
8 <!-- 配置JSON類型的result -->
9 <!--type屬性指定為"json"",將返回序列化的JSON格式數(shù)據(jù)-->
10 <result type="json">
11 <!-- <param name="root">user</param> -->
12 <param name="root">jsonResult
</param>
13
14 <!-- ??? -->
15 <param name="excludeProperties">data.loginPass
</param>
16 <param name="excludeNullProperties">true
</param>
17 </result>
18
19 </action>
20 </package>
21
22 <package name="default" namespace="/" extends="struts-default">
23 <action name="code" class="com.Elastic.StrutsDemo6.ivy.action.CodeAction">
24 <result type="stream">
25 <param name="ContentType">image/jpeg
</param>
26 <param name="inputStream"></param>
27 </result>
28
29 </action>
30
31 </package>
32 </struts>
3.UserAction類
1 package com.Elastic.StrutsDemo6.ivy.action;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import com.Elastic.StrutsDemo6.ivy.entity.User;
8 import com.opensymphony.xwork2.ActionContext;
9 import com.opensymphony.xwork2.ActionSupport;
10
11 public class UserAction
extends ActionSupport {
12 private User user;
13
14 private String code;
15
16 public String getCode() {
17 return code;
18 }
19
20 public void setCode(String code) {
21 this.code =
code;
22 }
23
24 public User getUser() {
25 return user;
26 }
27
28 public void setUser(User user) {
29 this.user =
user;
30 }
31
32 private Map<String, Object> jsonResult =
new HashMap<String, Object>
();
33
34 public Map<String, Object>
getJsonResult() {
35 return jsonResult;
36 }
37
38 public void setJsonResult(Map<String, Object>
jsonResult) {
39 this.jsonResult =
jsonResult;
40 }
41
42 public String login() {
43
44 long time = (
long) ActionContext.getContext().getSession().get("codeMaxTime"
);
45 if (time <
new Date().getTime()) {
46 jsonResult.put("msg", "驗證碼過期"
);
47 jsonResult.put("success",
false);
48 return SUCCESS;
49 }
50
51 String sessionCode = ActionContext.getContext().getSession().get("code"
).toString();
52 if (sessionCode.equalsIgnoreCase(code)) {
53 jsonResult.put("msg", "驗證碼錯誤"
);
54 jsonResult.put("success",
false);
55 return SUCCESS;
56 }
57
58 //登陸成功以后,額外添加的信息
59 /*jsonResult.put("success", true);
60 jsonResult.put("data", user);
61 jsonResult.put("msg", "登陸成功");
62 jsonResult.put("loginTime", new Date());*/
63
64 if ("admin".equals(user.getLoginName().trim()) && "123456"
.equals(user.getLoginPass().trim())) {
65 jsonResult.put("success",
true);
66 jsonResult.put("data"
, user);
67 jsonResult.put("msg", "登陸成功"
);
68 jsonResult.put("loginTime",
new Date());
69 }
else {
70 jsonResult.put("success",
false);
71 jsonResult.put("msg", "登陸失敗"
);
72 jsonResult.put("loginTime",
new Date());
73 }
74 return SUCCESS;
75 }
76
77 }
4.login.jsp
1 <%--引入JSP頁面PAGE指令 --%>
2 <%@ page language="java" contentType="text/html; charset=UTF-8"
3 pageEncoding="UTF-8"%>
4 <%-- 引入JSTL標簽指令 --%>
5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
6 <!DOCTYPE html>
7 <html language="zh-CN">
8 <head>
9 <meta charset="utf-8">
10 <!-- 設置瀏覽器渲染的引擎 -->
11 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12 <!-- 設置支持移動設備 -->
13 <meta name="viewport" content="width=device-width, initial-scale=1">
14 <title>網(wǎng)頁標題
</title>
15 <!-- 引入bootstrap的樣式 -->
16 <link rel="stylesheet" type="text/css"
17 href="<%=request.getContextPath()%>/css/bootstrap.min.css">
18 </head>
19 <body>
20 <div class="container">
21
22 <div class="panel panel-primary">
23 <div class="panel-heading">登陸
</div>
24 <div class="panel-body">
25 <form class="form-horizontal" action="login" method="post" data-role="login">
26 <div class="form-group">
27 <label class="col-md-3 control-label" for="loginName">用戶名:
</label>
28 <div class="col-md-6">
29 <input class="form-control" id="loginName" name="user.loginName" type="text" autocomplete="off" />
30 </div>
31 <div class="col-md-3">
32
33 </div>
34 </div>
35 <div class="form-group">
36 <label class="col-md-3 control-label" for="loginPass">密碼:
</label>
37 <div class="col-md-6">
38 <input class="form-control" id="loginPass" name="user.loginPass" type="password" />
39 </div>
40 <div class="col-md-3">
41
42 </div>
43 </div>
44 <div class="form-group">
45 <label class="col-md-3 control-label" for="code">驗證碼:
</label>
46 <div class="col-md-6">
47 <input class="form-control" id="code" name="code" type="password" />
48 <img id="codeImg" alt="" src="code" onclick="getCode()"><a href="javascript:getCode();">看不清楚,換一張?
</a>
49 </div>
50 <div class="col-md-3">
51
52 </div>
53 </div>
54 <div class="form-group">
55 <input class="btn btn-primary btn-block" data-role="login" type="button" value="登陸" />
56 </div>
57 </form>
58 </div>
59 </div>
60 </div>
61
62 <!-- 引入JS的樣式 -->
63 <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-2.2.4.js"></script>
64 <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script>
65
66 <script type="text/javascript" src="<%=request.getContextPath()%>/js/valid.js"></script>
67
68 <script type="text/javascript">
69 function getCode() {
70 document.getElementById('codeImg').src='code?_dc='+new Data();
71 }
72 </script>
73
74 <script type="text/javascript">
75 $(function() {
76 var valid = validForm($('form'), rules);
77
78 $('[data-role="login"]').click(function() {
79 //獲取表單
80 var $form = $(this).closest('form');
81 $.ajax({
82 url : "login",
83 type : "post",
84 data : $form.serialize(),
85 beforeSend : function() {
86 //返回方法
87 return valid();
88 },
89 success : function(result) {
90 alert(result.msg);
91 if (result.success) {
92 //window.location = 'index.jsp';
93 }
94 }
95
96 });
97 });
98 });
99 </script>
100 </body>
101 </html> ?
轉(zhuǎn)載于:https://www.cnblogs.com/ivy-xu/p/5648402.html
總結(jié)
以上是生活随笔為你收集整理的Struts(六)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。