Struts2中表单与Action传递数据三种方式
生活随笔
收集整理的這篇文章主要介紹了
Struts2中表单与Action传递数据三种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. Action中的屬性與表單中的屬性一致就可以JSP中的表單<formaction="login.action"method="post">用戶名:<inputtype="text"name="username"/> <br/>密碼: <inputtype="password"name="password"/><br/><inputtype="submit"value="登陸"/></form>Action中的屬性 publicclassLoginActionextends ActionSupport {private String username;private String password;public String getUsername() {returnusername;}publicvoid setUsername(String username) {this.username = username;}public String getPassword() {returnpassword;}publicvoid setPassword(String password) {this.password = password;}public String execute(){if( username.equalsIgnoreCase("aaa")&&password.equals("aaaaaa")){returnSUCCESS;}else{returnERROR;}}}2. 使用一個VO類在表單中提交的屬性名改為user.username<formaction="login.action"method="post">用戶名:<inputtype="text"name="user.username"/> <br/>密碼: <inputtype="password"name="user.password"/><br/><inputtype="submit"value="登陸"/></form>LoginAction中的屬性改為userpublicclassLoginActionextends ActionSupport{private User user;public User getUser() {returnuser;}publicvoid setUser(User user) {this.user = user;}public String execute(){if( user.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){returnSUCCESS;}else{returnERROR;}}}3. 使用Struts2中的ModelDriven數據模式Action類要實現一個泛型接口,前臺表單與1相同publicclassLoginActionextends ActionSupport implements ModelDriven<User> {private User user = new User();public String execute(){if( user.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){returnSUCCESS;}else{returnERROR;}}public User getModel() {returnuser;}}
總結
以上是生活随笔為你收集整理的Struts2中表单与Action传递数据三种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts2.0获取各种表单的数据
- 下一篇: struts2 hibernate登录