SpringMVC测试

这篇是接着上篇Spring单元测试写的。

一.修改web.xml,配置SpringMVC的前端控制器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="ticc_web" version="3.0">
 6     <servlet>
 7         <servlet-name>dispatcher</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <init-param>
10             <param-name>contextConfigLocation</param-name>
11             <param-value>classpath:spring.xml</param-value>
12         </init-param>
13         <load-on-startup>1</load-on-startup>
14     </servlet>
15     <servlet-mapping>
16         <servlet-name>dispatcher</servlet-name>
17         <url-pattern>/</url-pattern>
18     </servlet-mapping>
19 </web-app>

2.修改spring.xml,配置SpringMVC的视图解析器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7                http://www.springframework.org/schema/context
 8                http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9                http://www.springframework.org/schema/tx
10                http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
11                http://www.springframework.org/schema/aop
12                http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
13                ">
14     <!-- 自动扫描dao和service包(自动注入) -->
15     <context:component-scan base-package="com.youms.ssh.webService" />
16 
17     <!-- 配置视图解析器: 负责将视图名解析成真正的视图对象(比如jsp) -->
18     <bean
19         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="prefix" value="/WEB-INF/"></property>
21         <property name="suffix" value=".jsp"></property>
22     </bean>
23 </beans>

3.在WEB-INF下添加页面NewFile.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>Hello Spring!</h1>
11     
12     <h2>${test1}</h2>
13     <h2>${test2}</h2>
14 </body>
15 </html>

4.测试

 1 package com.youms.ssh.webService.controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.youms.ssh.webService.service.UserService;
13 
14 @Controller
15 @RequestMapping("/test")
16 public class Test {
17     @Autowired
18     private UserService userService;
19     
20     /**
21      * 测试普通的方法
22      * 此方法能接收到通过方法testRedirect1()重定向传来的参数
23      * 此方法能将通过方法testForward1()转发过来的参数传给页面
24      * @return
25      */
26     @RequestMapping(value="/testView",method=RequestMethod.GET)
27     public String testView(HttpServletRequest req,String test2){
28         System.out.println("进入TestAction");
29         System.out.println(test2+"=======================");
30         userService.test();
31         return "testView/NewFile";
32     }
33     
34     /**
35      * 测试使用ModelAndView传参
36      * @param res
37      * @return
38      */
39     @RequestMapping(value="/testModel",method=RequestMethod.GET)
40     public ModelAndView testModel(HttpServletResponse res){
41         //与页面编码保持一致则不会乱码
42         res.setCharacterEncoding("utf-8");
43         ModelAndView mav = new ModelAndView("testView/NewFile");
44         mav.addObject("test1","测试1!");
45         return mav;
46     }
47     
48     /**
49      * 测试使用ModelAndView重定向
50      * @param res
51      * @return
52      */
53     @RequestMapping(value="/testRedirect1",method=RequestMethod.GET)
54     public ModelAndView testRedirect1(HttpServletResponse res){
55         //与页面编码保持一致则不会乱码
56         res.setCharacterEncoding("utf-8");
57         ModelAndView mav = new ModelAndView("redirect:/test/testView");
58         mav.addObject("test2","测试2!");
59         return mav;
60     }
61     
62     /**
63      * 测试使用字符串重定向
64      * @param res
65      * @return
66      */
67     @RequestMapping(value="/testRedirect2",method=RequestMethod.GET)
68     public String testRedirect2(HttpServletResponse res){
69         return "redirect:/test/testView";
70     }
71     
72     /**
73      * 测试使用ModelAndView转发
74      * @param res
75      * @return
76      */
77     @RequestMapping(value="/testForward1",method=RequestMethod.GET)
78     public ModelAndView testForward1(HttpServletResponse res){
79         //与页面编码保持一致则不会乱码
80         res.setCharacterEncoding("utf-8");
81         ModelAndView mav = new ModelAndView("forward:/test/testView");
82         mav.addObject("test2","测试2!");
83         return mav;
84     }
85     
86     /**
87      * 测试使用字符串转发
88      * @param res
89      * @return
90      */
91     @RequestMapping(value="/testForward2",method=RequestMethod.GET)
92     public String testForward2(HttpServletResponse res){
93         return "forward:/test/testView";
94     }
95 }

 

posted @ 2016-10-27 09:39  执迷而不悔  阅读(672)  评论(0编辑  收藏  举报