Spring In Action SpringMVC 提交表单

首先定义一个form对象
写道

public class UserForm implements Serializable{

/**
*
*/
private static final long serialVersionUID = 6689428704046325510L;

private String name;

private String email;

private String password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "UserForm [name=" + name + ", email=" + email + ", password="
+ password + "]";
}

 2-对应的form页面

写道
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@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=ISO-8859-1">
<title>form submit</title>
</head>
<body>
<form:form action="query" method="post">
name: <input type="text" name="name">
<br /> password: <input type="password" name="password">
<br /> email: <input type="text" name="email">
<br />
<input type="submit">
</form:form>
</body>
</html>

 3-对应controller

写道
package com.spring.mvn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.mvn.form.UserForm;
@Controller
public class QueryController {

@RequestMapping(value = "query", method = RequestMethod.POST)
public String query(@ModelAttribute("form") UserForm form) {
System.out.println("form->" + form);
return "welcome";

}
}

 测试:

url:http://localhost:8080/mvc/goForm

输入信息,提交

结果:

form->UserForm [name=zhangsan, [email protected], password=lisi]

你可能感兴趣的:(springMVC)