- action中的属性名称与JSP中提交的name值一致
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;
}
。。。。。。。。。。。。。。。
}
- 使用一个VO类
JSP中的表单
<formaction="login.action"method="post">
用户名:<inputtype="text"name="user.username"/> <br/>
密码: <inputtype="password"name="user.password"/><br/>
<inputtype="submit"value="登陆"/>
</form>
LoginAction中的属性改为user
publicclassLoginActionextends ActionSupport{
private User user;
public User getUser() {
returnuser;
}
publicvoid setUser(User user) {
this.user = user;
}
- 使用Struts2中的ModelDriven数据模式
Action类要实现一个泛型接口
JSP中的表单
<formaction="login.action"method="post">
用户名:<inputtype="text"name="username"/> <br/>
密码: <inputtype="password"name="password"/><br/>
<inputtype="submit"value="登陆"/>
</form>
publicclassLoginActionextends ActionSupport implements ModelDriven<User> {
private User user = new User();
public User getModel() {
returnuser;
}
}