spring笔记-spring mvc表单

Spring MVC表单处理例子

 

由web.xml接收jsp的请求

<display-name>Spring MVC Form Handling</display-name><servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet><servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

再将请求发送给controller(StudentController.java)

@Controller
public class StudentController {
   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }   
   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());      
      return "result";
   }
}

接收来自浏览器的student请求,用map返回student对象

接收来自浏览器的addStudent请求,用mapmodel盛装三个值

注解说明

@ModelAttribute用在方法和参数上,该方法就是把参数放进model中,标注参数类型。标注了@ModelAttribute会在@RequestMapping之前被调用。

类说明

Model是一个接口,继承了ModelMap类

ModelMap主要用于传递控制方法数据到结果页面(和setAttribute功能相似),本身不能实现页面跳转,页面上可以用el ,$key,bboss获取。

ModelAndView 1 .设置跳转地址2.传递控制方法数据到结果页面3. 页面上可以用el ,$key,bboss获取。

controller结果发送到xxx-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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"><context:component-scan base-package="com.tutorialspoint" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean></beans>

view添加学生信息

<%@ 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 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    --></head>
  
  <body>
    <h2>Student Information</h2>
 <form method="GET" action="/spring/addStudent">
<tr>
<td><input type="text" name="name"></td>
<td>    <input type="text" name="age"></td>
<td>    <input type="text" name="id"></td>
<td>    <input type="submit" value="submit"></td>
</tr>
</form>
  </body>
</html>

表单键值传递到addStudent,controller接收到addStudent命令,把键值放入ModelMap中,返回到result页面。

view显示学生信息

<%@ 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 'student.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
 <h2> Submitted Student Information </h2>
   <table>
    <tr>
        <td>Name</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>Age</td>
        <td>${age}</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>${id}</td>
    </tr>
</table>  
  </body>
</html>

  


 

原文地址:https://www.cnblogs.com/1605-3QYL/p/12564966.html