zoukankan      html  css  js  c++  java
  • MyEclipse Spring 学习总结三 SpringMVC 表单处理

    SpringMVC 表单处理的项目结构如下图所示:

    Student.java 文件

    public class Student {
    	private Integer age;
    	private String name;
    	private Integer id;
    
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    
    	public Integer getAge() {
    		return age;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setId(Integer id) {
    		this.id = id;
    	}
    
    	public Integer getId() {
    		return id;
    	}
    }
    

      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("TestSpringMVC")Student student, 
       ModelMap model) {
          model.addAttribute("name", student.getName());
          model.addAttribute("age", student.getAge());
          model.addAttribute("id", student.getId());      
          return "result";
       }
    }
    

      web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    	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_3_0.xsd">
      <display-name>Spring MVC Application</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>
    </web-app>
    

      HelloWeb-servlet.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
    	xmlns:p="http://www.springframework.org/schema/p"
    	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.example" />
    
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp" />
       </bean>
     
    </beans>
    

      student.jsp文件

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <html>
    <head>
        <title>Spring MVC Form Handling</title>
    </head>
    <body>
    
    <h2>Student Information</h2>
    <form:form method="POST" action="/TestSpringMVC/addStudent">
       <table>
        <tr>
            <td><form:label path="name">Name</form:label></td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td><form:label path="age">Age</form:label></td>
            <td><form:input path="age" /></td>
        </tr>
        <tr>
            <td><form:label path="id">id</form:label></td>
            <td><form:input path="id" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Submit"/>
            </td>
        </tr>
    </table>  
    </form:form>
    </body>
    </html>
    

      result.jsp文件

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <html>
    <head>
        <title>Spring MVC Form Handling</title>
    </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>
    

     运行效果如下:

    输入数值后,点击Submit

  • 相关阅读:
    Python自动化学习笔记(九)——Python的面向对象
    Python自动化学习笔记(八)——接口开发、发送网络请求、发送邮件、写日志
    MRWordCount
    环境变量追加命令
    hadoop退役旧数据节点
    Hadoop服役新数据节点
    Namenode文件损坏
    NameNode故障处理
    NN和2NN工作机制
    hdfs读写流程
  • 原文地址:https://www.cnblogs.com/linlf03/p/5180169.html
Copyright © 2011-2022 走看看