再也不用 getParamter 了
1. 建一个User类
package com.xinzhi.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; /** * @author sr * @date 2021/1/25 */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String username; private String password; }
2. 修改FirstController.java
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addUser(User user) throws Exception {
//模型里封装数据
System.out.println(user);
return "hellomvc";
}
要什么,加什么
@RequestMapping(value = "/add",method = RequestMethod.POST) public String addUser(User user,HttpServletRequest request) throws Exception { //模型里封装数据 request.setAttribute("user",user); System.out.println(user); return "add"; }
或这样
public class FirstController { @RequestMapping(value = "/add") public String addUser(Integer id,String username,String password,HttpServletRequest request) throws Exception { //模型里封装数据 request.setAttribute("user",new User(id,username,password)); return "hellomvc"; }
但如果这个参数名跟user类的私有变量不一样,就传不过去
解决方法:
3. 在index.jsp里面做个表单
<form action="/user/add" method="post"> <input name="id" > <input name="username"> <input name="password"> <input type="submit" value="提交"> </form>
4.测试
成功