最近去面试有两次问道了SpringMVC的返回值,第一次没答上来了,第二次也没答上来了,因为项目中用的SpringMVC的返回值类型是ModelAndView,回答的时候只说了String和ModelAndView。回来查了一下知道SpringMVC的返回值类型,SpringMVC的返回值类型有ModelAndView,String,void,Map,Model,下面是实现代码。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- classpath*:springController.xml -->
classpath*:springControllerZhujie.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springDAO.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
SpringMVC配置文件springControllerZhujie.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 将 com.wangbiao.springMVC包下带有注解的java类放入SpringIOC容器中,生成实例-->
<context:component-scan base-package="com/wangbiao/springMVC"></context:component-scan>
<!-- 对应分别提供了AbstractHandlerMethodMapping , AbstractHandlerMethodAdapter 主要作用相当于HandleMapping,HandleAdapter-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- webroot到一指定文件夹文件路径 -->
<property name="prefix" value="/"/>
<!-- 视图名称后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
package com.wangbiao.springMVC;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
@RequestMapping("/return")
public class HelloWorld extends MultiActionController{
/*
* 返回值为ModelAndView类型的
*/
public ModelAndView sayHelloWorld(HttpServletRequest request, HttpServletResponse response) {
String param = request.getParameter("param");
System.out.println("springMVC测试:helloWorld;"+param);
ModelAndView mv = new ModelAndView();
User user = (User) this.getApplicationContext().getBean("user");
mv.addObject("content", "springMVC HelloWorld:"+param);
mv.setViewName("springMVC/helloWorld");
ServletContext ctx = this.getServletContext();
return mv;
}
/*
* 返回值为String类型的,视图名就是返回值
*/
@RequestMapping(value="/returnString",method=RequestMethod.GET)
public String returnString(Model model) {
model.addAttribute("test", "return string!!!");
System.out.println("springMVC测试:helloWorld;");
return "return/returnString";
}
/*
* 返回值是void类型的,由于没有返回值
* 它默认的展示视图名和url中的一段是一样的即returnVoid.jsp
*/
@RequestMapping("/returnVoid")
public void returnVoid(Model model){
model.addAttribute("test", "return void!!!");
}
/*
* 返回值是void类型的,由于没有指定视图名
* 它默认的展示视图名和url中的一段是一样的即returnVoid.jsp
*/
@RequestMapping("/returnModel")
public Model returnModel(Model model){
model.addAttribute("test", "return Model!!!");
return model;
}
/*
* 返回值是void类型的,由于没有指定视图名
* 它默认的展示视图名和url中的一段是一样的即returnVoid.jsp
*/
@RequestMapping("/returnMap")
public Map returnMap(Model model){
Map map = new HashMap<String,String>();
map.put("test", "return map!!!");
return map;
}
}
returnString.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'returnString.jsp' starting page</title>
</head>
<body>
123456 ${test }<br>
</body>
</html>
运行链接
http://3g.fang.com:8000//SpringMVC/return/returnString
显示