springmvc model层添加数据
conteroll:
package com.qfedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.edu.demo.User;
//import com.edu.demo.User;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/form")
public String form() {
return "user/form";
}
@RequestMapping("/save")
// 如果是对象接收返回就是对象,如果是name字段接收返回就是变量
public String save(User user) {
System.out.println(user);
return "user/success";
}
}
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="save" method="post" >
<label for="">用户名:<input type="text" name="username"/></label>
<label for="">密码:<input type="password" name="password"/></label>
<label for="">邮箱:<input type="text" name="email"/></label>
<label for="">年龄:<input type="text" name="age"/></label>
<label for="">省份:<input type="text" name="address.province"/></label>
<label for="">城市:<input type="text" name="address.city"/></label>
<button>保存</button>
</form>
</body>
</html>
user类,做数据库接收的对象类
package com.edu.demo;
public class User {
private String username;
private String password;
private String email;
private Integer age;
private Address address;
public User() {
super();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

浙公网安备 33010602011771号