spring mvc:文本框

采用:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>设置,来使用form文本框

我的项目名称是hello, 在src/main/java下面建了一个目录chapter2: src/main/java/chapter2/

以学习姓名,年龄,id号为例。

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

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;


@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 "student_result";
		
	}
	
	
	
}

  

这里的第一个服务方法student(),我们已经在ModelAndView对象中传递了一个名称为“command”的空对象,因为如果要在JSP中使用<form:form>标签,spring框架需要一个名称为“command”的对象文件。当调用student()方法时,它返回student.jsp视图。

第二个服务方法addStudent()将在URL HelloWeb/addStudent 上的POST方法调用。将根据提交的信息准备模型对象。 最后,将从服务方法返回“result”视图,这将渲染result.jsp 页面。

student.jsp的代码如下所示 

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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>Spring MVC表单处理</title>
</head>
<body>


<h2>Student Information</h2>
<form:form method="post" action="/hello/addStudent">
<table>
	<tr>
		<td><form:label path="name">姓名:</form:label></td>
		<td><form:input path="name"/></td>
	</tr>
	<tr>
		<td><form:label path="age">年龄</form:label></td>
		<td><form:input path="age"/></td>
	</tr>
	<tr>
		<td><form:label path="id">编号</form:label></td>
		<td><form:input path="id"/></td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="submit" value="提交表单"/>
		</td>
	</tr>
</table>
</form:form>

</body>
</html>

  student_result.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>
<!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>spring mvc表单处理</title>
</head>
<body>


<h2>提交的学生信息</h2>
<table>
	<tr>
		<td>名称:</td>
		<td>${name}</td>
	</tr>
	<tr>
		<td>年龄:</td>
		<td>${age}</td>
	</tr>
	<tr>
		<td>编号:</td>
		<td>${id}</td>
	</tr>
</table>

</body>
</html>

  

注意加入jstl标签库,否则不会解析的。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>

  

原文地址:https://www.cnblogs.com/achengmu/p/8889051.html