配置 web.xml ,加入spring mvc,代码如下:
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| <filter>
<filter-name>opensessioninview</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>opensessioninview</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>spring_mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/config/spring/spring-actions.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring_mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/config/spring/spring*.xml
</param-value>
</context-param>
|
当然,在 spring配置文件中,还要加上如下配置:
1
2
| <!-- 将静态资源交默认处理器去操作 -->
<mvc:default-servlet-handler/>
|
下面贴出登录系统的服务器端代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| /**
* 处理登录应用控制器
* @author admin
*
*/
@Controller
public class LoginAction {
@Resource
private ObjectMapper om ;
@Resource
private ISystemManagerService systemManagerService;
@RequestMapping(value="loginSystem",method=RequestMethod.GET)
public void login(@RequestParam("code") String loginCode,
@RequestParam("pwd") String password,
HttpServletResponse response,
HttpSession session
) throws Exception {
response.setCharacterEncoding("utf-8");
MyResponse resp = new MyResponse();
try {
User u = systemManagerService.login(loginCode, password);
resp.setData(u);
session.setAttribute("session_key", u);
} catch (Exception e) {
resp.setCode(MyResponse.ERROR);
resp.setMsg(e.getMessage());
}
om.writeValue(response.getOutputStream(), resp);
}
|
大家要注意,我们使用的是jackson返回数据,而没有用spring mvc所集成的@ResponseBody,主要是想自己更为灵活地控制 json的生成。
MyResponse是定义的统一数据返回格式,代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| public class MyResponse {
public static final int ERROR = 0;
public static final int SUCCESS = 1;
private int code = SUCCESS;
private Object data;
private String msg = "";
public void setErrorCode() {
code = this.ERROR;
}
.....
}
|
客户端代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn_login").click(function() {
$.ajax({
type:"get",
url:"loginSystem",
dataType:"json",
data:"code=" + $("input[name='code']").val() + "&pwd=" + $("input[name='pwd']").val(),
success:function(result) {
if (result.code == 0 ) {
alert(result.msg);
} else {
alert("恭喜" + result.data.name + "登录成功!");
window.location.href = "userManager/main.html";
}
}
});
});
});
</script>
|
1
2
3
4
5
| <form action="">
用户名:<input type="text" name="code"><br/>
密码:<input type="password" name="pwd"><br/>
<input type="button" value="登录" id="btn_login">
</form>
|
本文转载于:http://yanyaner.com/blog/2012/12/28/restful2/
刘江华的博客