springmvc 如何把获取的数据返回到页面呢?
下面进行小测试
通过HttpServletRequest
@RequestMapping(value="returnValue1.do")
public String returnValue1(HttpServletRequest request){
request.setAttribute("user", "HttpServletRequest");
return "value";
}
2.通过ModelAndView
// 通过ModelAndView 返回值
@RequestMapping(value="returnValue2.do")
public ModelAndView returnValue2(){
ModelAndView model = new ModelAndView("value");
model.addObject("user", "ModelAndView");
return model;
}
3.通过Model
// 通过Model 返回
@RequestMapping(value="returnValue4.do")
public String returnValue4(Model model){
model.addAttribute("user", "Model");
return "value";
}
4.通过Map
// 通过Model 返回
@RequestMapping(value="returnValue5.do")
public String returnValue5(Map map){
map.put("user", "Map");
return "value";
}
5.项目结构
6.完整TestValue代码
package com.control;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.entity.User;
@Controller
@RequestMapping("/user/")
public class TestValue {
// 通过HttpServletRequest 设置作用域返回值
@RequestMapping(value="returnValue1.do")
public String returnValue1(HttpServletRequest request){
request.setAttribute("user", "HttpServletRequest");
return "value";
}
// 通过ModelAndView 返回值
@RequestMapping(value="returnValue2.do")
public ModelAndView returnValue2(){
ModelAndView model = new ModelAndView("value");
model.addObject("user", "ModelAndView");
return model;
}
// 通过ModelAndView 返回 对象
@RequestMapping(value="returnValue3.do")
public ModelAndView returnValue3(){
ModelAndView model = new ModelAndView("value");
User user = new User("张三", "china", 18);
model.addObject("user", user);
return model;
}
// 通过Model 返回
@RequestMapping(value="returnValue4.do")
public String returnValue4(Model model){
model.addAttribute("user", "Model");
return "value";
}
// 通过Model 返回
@RequestMapping(value="returnValue5.do")
public String returnValue5(Map map){
map.put("user", "Map");
return "value";
}
}
7.完整User
package com.entity;
public class User {
private String uName;
private String address;
private Integer age;
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String uName, String address, Integer age) {
super();
this.uName = uName;
this.address = address;
this.age = age;
}
public User() {
super();
}
@Override
public String toString() {
return "User [uName=" + uName + ", address=" + address + ", age=" + age + "]";
}
}
8.完整utf8
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/utf8")
public class utf8 implements Filter {
public utf8() {
}
public void destroy() {
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
}
9.完整value.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${user }</h1>
</body>
</html>
10.完整springmvc-servlet.xml配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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-3.1.xsd">
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 注解扫描包 -->
<context:component-scan base-package="com.control"/>
<!-- 视图控制器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
11.完整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_2_5.xsd" version="2.5">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
12.完整index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 通过HttpServleteRequest返回 -->
<a href="user/returnValue1.do">HttpServleteRequest</a><br/>
<!-- 通过ModelAndView 返回值 -->
<a href="user/returnValue2.do">ModelAndView</a><br/>
<!-- 通过ModelAndView 返回对象 -->
<a href="user/returnValue3.do">Object</a><br/>
<!-- 通过Model 返回 -->
<a href="user/returnValue4.do">Model</a><br/>
<!-- 通过Ma5 返回 -->
<a href="user/returnValue5.do">Map</a><br/>
</body>
</html>
13.关羽springmvc的跳转方式?
在springmvc默认跳转方式是 请求转发
return "login";// 请求转发 默认方式,去视图解析中拼接
重定向jsp
return "redirect:/view/success.jsp"; // 这里是文件全名
重定向Action
return "redirect:/view/aa";
小小测试,以备忘记的时候,可以有个参考。如有不足,还望大家多多指教。
转载于:https://blog.51cto.com/zhuws/1924637