SpringMVC学习笔记(二)--ModelAndView绑定值的方式,以及前端页面如何接受其绑定的值
声明:这段时间做SpringMVC项目,在控制器接受前端界面的值,向前端界面传送值方面和前端界面接受值方面花了很长时间,也看了一些博客,现在总结下来。
1:login.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> <form action = "${pageContext.request.contextPath}/test" method = "post"> 用户名:<input type = "text" id = "name" name = "name" value = ""/> <input type = "submit" value = "测试"/> </form> </body> </html>
2:UserController控制器
package com.test.controller; import java.util.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; 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.RedirectView; @Controller//这个必须要加,否则控制器接收不到前端发来的请求 public class UserController { @RequestMapping(value="/test",method = RequestMethod.POST) public ModelAndView test(ModelAndView mv, HttpServletRequest request,HttpServletResponse response) throws Exception { //绑定值的三种方式 //第一种方式 String name = request.getParameter("name"); //System.out.println(name); mv.addObject("username", name);//绑定值 //第二种方式 // List List<String> list = new ArrayList<String>(); list.add("java"); list.add("python"); list.add("c++"); mv.addObject("BookList", list); //第三种方式 // Map Map<String,String> map = new HashMap<String, String>(); map.put("beijing", "北京"); map.put("lisi", "成都"); map.put("wangmazi", "西安"); mv.addObject("map", map); //绑定传送地址的两种方式 //第一种方式 mv.setViewName("success"); //第二种方式 不知道为什么在前端接受不到username的值, // mv.setView(new RedirectView("success.jsp")); return mv; } }
3.success,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"> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>name_el:${username}</h1> <% String name = request.getAttribute("username").toString();%> <h2>name_java:<%=name%></h2> <h3>name_java_el:${name}接收不到</h3> <!-- 输出List --> <p>书籍列表</p> <c:forEach items="${BookList}" var="node"> <c:out value="${node}"></c:out> </c:forEach> <br/> <br/> <!-- 输出Map --> <c:forEach items="${map}" var="node"> 姓名:<c:out value="${node.key}"></c:out> 住址:<c:out value="${node.value}"></c:out> <br/> </c:forEach> </body> </html>
4.Spring.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:task="http://www.springframework.org/schema/task" 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:tx="http://www.springframework.org/schema/tx" 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.xsd"> <!-- 自动扫描(自动注入)开启注释$Controller --> <context:component-scan base-package="com.test.controller"/> <!--内部视图解析器,JSP与JSTL模板 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--指定视图渲染类 --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!--自动添加到路径中的前缀 --> <property name="prefix" value="/" /> <!--自动添加到路径中的后缀 --> <property name="suffix" value=".jsp" /> <!--设置所有视图的内容类型,如果视图本身设置内容类型视图类可以忽略 --> <property name="contentType" value="text/html;charset=UTF-8" /> </bean> </beans>
5.登录:
6.显示:
7.解释一下:
spring的MVC是对Servlet的封装,ModelAndView中addObject应该是对request.setAttribute方法的封装,所以在jsp中如果想以el表达式来读取数据,应该用${requestScope.depts}或者${depts},对应于<%=request.getAttribute("depts").toString();%>