ssh项目从前端传值到后台

1.单个属性的话就添加该属性的set get方法

2.对象的话添加对象的set get方法

public class UserAction {
    private User user;
    public String add(){
       new UserManager().addUser(user);
       return "success";
    }
    public User getUser() {
       return user;
    }
    public void setUser(User user) {
       this.user = user;
    }

}
add_input.jsp: 缺点这种做法不好的地方是:缺点JSP页面上表单域中的命名变得太长
<form action="test/user.action" method="post">
        <input type="hidden" name="method:add">
        username:<input type="text" name="user.username"> <br/>
        password:<input type="text" name="user.password"> <br/>
        age:<input type="text" name="user.age"> <br/>
        address:<input type="text" name="user.address"> <br/>
        <input type="submit" name="submit" value="添加用户">
     </form> <br/>
利用ModelDriven机制,让UserAction实现一个ModelDriven接口,同时实现接口中的方法:getModel()
public class UserAction implements ModelDriven{
    private User user;
    @Override
    public Object getModel() {
       if(user == null){
           user = new User();
       }
       return user;
    }
    public String add(){
       new UserManager().addUser(user);
       return "success";
    }
    public User getUser() {
       return user;
    }
    public void setUser(User user) {
       this.user = user;
    }
}
 <form action="test/user.action" method="post">
        <input type="hidden" name="method:add">
        username:<input type="text" name="username"> <br/>
        password:<input type="text" name="password"> <br/>
        age:<input type="text" name="age"> <br/>
        <input type="submit" name="submit" value="添加用户">
     </form> <br/>




阅读更多

没有更多推荐了,返回首页