package com.djoker.struts2; public class User { private String username; private String password; 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; } }
package com.djoker.struts2; import com.opensymphony.xwork2.ModelDriven; public class LoginAction implements ModelDriven<User>{ private User user = new User(); public String execute() throws Exception { if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){ return "success"; } return "error"; } @Override public User getModel() { return user; } }
package com.djoker.struts2; public class LoginAction{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute() throws Exception { if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){ return "success"; } return "error"; } }
jsp页面中的form表单书写方式
<form action="login.action" method="post"> 用户名:<input type="text" name="user.username"><br> 密 码:<input type="password" name="user.password"><br> <input type="submit" value="登录"> </form>
原文:http://www.cnblogs.com/djoker/p/6218524.html