1. 程式人生 > >ssh專案從前端傳值到後臺

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/>