Controller里的方法返回值
若Controller
里的方法处理完后需要跳转到其它页面,在跳转的同时需要传递数据,此时可以使用ModelAndView
Controller
里的方法可以返回String,该String可以是逻辑视图名也可以是物理视图名;逻辑视图名需要通过视图解析器转换
示例:
@Controller
@RequestMapping(value = "/security")
public class PasswordSecurityController {
@RequestMapping(value = "/addMiBao.do")
public void addPasswordProtected(String question, String answer){
System.out.println("question = " + question);
System.out.println("answer = " + answer);
}
@RequestMapping(value = "/addMiBao.do", method = RequestMethod.POST)
public String addPasswordProtected(PwdProtectData data){
System.out.println(data.getQuestion());
System.out.println(data.getAnswer());
System.out.println(data.getName());
System.out.println(data.getUser());
return "/index.html";
}
}
上面代码中如果浏览器向服务器发送post请求,当请求URL为http://localhost:8080/项目名/security/addMiBao.do
时,请求的方法是String类型,结果将返回web项目下的index.html
页面,默认的视图解析器会把字符串*转换为对应的页面
Controller
里方法的返回值为void主要用于ajax
请求,这类请求一般不用返回页面,只需要返回数据,在方法参数里添加HttpServletResponse
参数后可以通过HttpServletResponse
往前台写入数据
示例代码:
由于示例中使用了JSONArray
,所有要添加json-lib
的依赖,依赖内容如下:
<!-- *********************************************************************-->
<!-- json依赖的jar包 -->
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.3</version>
<classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<!-- *********************************************************************-->
示例配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.hurrican.*"/>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 注册中央调度器-->
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springMVC的配置文件及配置名-->
<param-name>contextConfigLocation</param-name>
<!--<param-value>classpath:spring-mvc.xml</param-value>-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 为userController配置路径映射-->
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
关键代码
以对象形式整体接收
- Controller里的方法也可以返回
Object
,返回的Object
不是作为逻辑视图出现,而是直接在页面显示数据 - 要返回
Object
对象需要使用@ResponseBody
注解,将转换后的JSON 数据放入到响应体中 依赖的jar包:
jackson-annotation-2.5.0.jar
jackson-core-2.5.0.jar
jackson-databind-2.5.0.jar
注册注解驱动 :将
Object
数据转换为JSON
数据,需要由http消息转换器HttpMessageConveter完成,开启转换器需要在spring配置文件中添加<mvc:annotation-driven />
注
- 当
Controller
里的方法返回结果是Object
并且结果含有中文时可能会出现中文乱码,需要在@RequestMapping
注解里配置 produces 属性的值,因为过滤器把request
和response
的编码都设置为utf-8,所以一般把produces
设置为text/html;charset=utf-8
- 当前端提交的数据的名称与
Controller
里的方法参数的名字不一致时需要用@RequestParam
注解进行映射(比如:前台提交的参数名为new_email,后台控制器方法里的参数却为email,可以在方法参数名前面加上@RequestParam(new_email)
进行映射)
maven依赖
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>