今天做了一个返回值的练习,将后台的数据返回给前段页面。
下面将实例进行记录,方便后期自己的查找。
环境:
JDK1.8
Tomcat:8.0
win7
第一步:创建一个web项目并添加相关jar
项目目录如下图:
jsp页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="reg5.do"> <input type="submit" value="提交" /> </form> <br> <div> 姓名:${name1 }<br> ${list1 }<br> </div> </body> </html>
第二步:配置web.xml
web.xml代码如下,里面的加了一个拦截器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>springmvc3test</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc3</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc3</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
第三步:配置springmvc核心配置:springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- springmvc3的扫描驱动 --> <context:component-scan base-package="com.zgy.controller"></context:component-scan> <!-- 试图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> //返回原界面 <property name="suffix" value=".jsp"></property> </bean> </beans>
第四步:编写UserInfo实体类:
package com.zgy.domain; public class UserInfo { public Integer id; public String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "用户ID=" + id + ", 用户名=" + name + ","; } }
第五步:编写控制器类RegController
package com.zgy.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; 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.zgy.domain.UserInfo; @Controller public class RegController { @RequestMapping("/reg.do") public String queryUser1(HttpServletRequest request) { String name="华总管"; request.setAttribute("name1",name); UserInfo user = new UserInfo(); user.setId(9527); user.setName("华安"); request.setAttribute("user",user); return "index"; } @RequestMapping("/reg2.do") public String queryUser2(Map<String,Object> map) { UserInfo user = new UserInfo(); user.setId(1002); user.setName("石榴"); map.put("user3",user); return "index"; } @RequestMapping("/reg3.do") public String queryUser3(Model model) { UserInfo user = new UserInfo(); user.setId(1); user.setName("华夫人"); model.addAttribute("user3",user); return "index"; } @RequestMapping("/reg4.do") public String queryUser4(Model model) { UserInfo user = new UserInfo(); user.setId(1); user.setName("华夫人"); List<UserInfo> list = new ArrayList<UserInfo>(); list.add(user); model.addAttribute("list1",list); return "index"; } }