SpringMVC Form表单

学习参考自易百教程

1、新建javaWeb工程springMVC_Form,选择Web服务器

2、点击next,取消生成index.jsp,勾选web.xml,取消导入jstl库

3、点击finish完成,右键项目,部署spring开发

取消勾选该页所有选择

4、点击finish完成。接下来编写student类和controller控制器添加dispatcher-servlet.xml,完成web.xml和新建jsp文件,目录如下

扫描二维码关注公众号,回复: 2582632 查看本文章

FormController:

package com.controller;

import com.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FormController {

	@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,Model model){
		model.addAttribute("sno",student.getSno());
		model.addAttribute("sname",student.getSname());
		model.addAttribute("gender", student.getGender());
		model.addAttribute("score",student.getScore());
		return "result";
	}
}

Student类:

package com.model;

public class Student {
	private String sno;
	private String sname;
	private String gender;
	private double score;
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
}

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>springMVC_hello</display-name>
  
  <servlet>
  	<servlet-name>dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  	</init-param>
  </servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  	
</web-app>

dispatcher-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"
   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.controller"/>
   
   	<!-- <mvc:default-servlet-handle />
   	<mvc:annotation-driven /> -->
   	
   	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/jsp/" />
      <property name="suffix" value=".jsp" />
   	</bean>
   
</beans>

student.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>SpringMVC studentAdd</title>
  </head>
  
  <body>
    <h2>SpringMVC Form</h2>
    <form:form action="/springMVC_form/addStudent" method="post">
    	<!-- http://localhost:8080/springMVC_form/addStudent 
    	访问路径须填写完整,或者requestMapping须映射根路径-->
    	<table>
    	
    		<tr>
    			<td><form:label path="sno">学号</form:label></td>
    			<td><form:input path="sno"/></td>
    		</tr>
    		<tr>
    			<td><form:label path="sname"/>名字</td>
    			<td><form:input path="sname"/></td>
    		</tr>
    		<tr>
    			<td><form:label path="gender">性别</form:label></td>
    			<td><form:input path="gender"/></td>
    		</tr>
    		<tr>
    			<td><form:label path="score">分数</form:label></td>
    			<td><form:input path="score"/></td>
    		</tr>
    		<tr>
    			<td colspan="2">
    				<input type="submit" value="submit">
    				<input type="reset" value="reset">
    			</td>
    		</tr>
    		
    	</table>
    </form:form>
  </body>
</html>

result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>SpringMVC getStudentInfo</title>
  </head>
  
  <body>
    <h2>Student Info</h2>
    <table>
    	<tr>
    		<td>sno:${sno}</td>
    	</tr>
    	<tr>
    		<td>sname:${sname}</td>
    	</tr>
    	<tr>
    		<td>gender:${gender}</td>
    	</tr>
    	<tr>
    		<td>score:${score}</td>
    	</tr>
    </table>
  </body>
</html>

结果:

猜你喜欢

转载自blog.csdn.net/zero_130/article/details/81347631