1、Controller方法的返回类型
(1)ModelAndView
需要在方法结束时定义ModelAndView并设置model和view,然后返回ModelAndView对象。
(2)String
表示方法返回逻辑视图名,真正视图=前缀+逻辑视图名+后缀。
(3)void
方法的参数包含请求对象和响应对象。
2、实例
(1)项目结构
(2)在web.xml文件中配置前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC22</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
(3)在spring-servlet.xml文件中配置处理器映射器、处理器适配器、视图解析器和Handler处理器
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.hello.controller" />
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/page/" />
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
(4)编写Handler处理器HelloController
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hello1.action")
public ModelAndView hello1() throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "Hello1");
modelAndView.setViewName("hello");
return modelAndView;
}
@RequestMapping("/hello2.action")
public String hello2(Model model) throws Exception {
model.addAttribute("message", "Hello2");
return "hello";//返回逻辑视图名
}
@RequestMapping("/hello3.action")
public String hello3() throws Exception {
return "redirect:hello1.action";//重定向(request不可以共享)
}
@RequestMapping("/hello4.action")
public String hello4() throws Exception {
return "forward:hello1.action";//转发(request可以共享)
}
@RequestMapping("/hello5.action")
public void hello5(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.sendRedirect("hello1.action");//重定向
}
@RequestMapping("/hello6.action")
public void hello6(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("hello1.action").forward(request, response);//转发
}
@RequestMapping("/hello7.action")
public void hello7(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
}
}
(5)hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>
(6)结果
http://localhost:8080/SpringMVC22/hello/hello1.action
http://localhost:8080/SpringMVC22/hello/hello2.action
http://localhost:8080/SpringMVC22/hello/hello3.action
http://localhost:8080/SpringMVC22/hello/hello4.action
http://localhost:8080/SpringMVC22/hello/hello5.action
http://localhost:8080/SpringMVC22/hello/hello6.action
http://localhost:8080/SpringMVC22/hello/hello7.action