日期:2014-05-19  浏览次数:20680 次

关于struts2在页面取值的问题
这是我的修改界面的表单
HTML code

<form action="user/update" method="post">
    <table border="0" align="center" width="700">
        <tr height="30"><td colspan="2"><s:debug/></td></tr>
        <tr height="30"><td colspan="2"><s:fielderror/></td></tr>
        <tr height="30"><td>姓:</td><td><input type="text" name="firstname" value="${firstname}"></td></tr>
        <tr height="30"><td>名:</td><td><input type="text" name="secondname" value="${secondname}"></td></tr>
        <tr height="30"><td>年龄</td><td><input type="text" name="age" value="${age}"></td></tr>
        <tr height="30"><td colspan="2"><input type="submit" value="submit">&nbsp;&nbsp;<input type="reset" value="reset"></td></tr>
    </table>
</form>


这是我修改操作的action,我访问
Java code

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hktk.entity.user.User;
import com.hktk.service.user.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("updateUserAction")
@Scope("prototype")
public class UpdateUserAction extends ActionSupport implements ModelDriven<User>{

    private static final long serialVersionUID = 1L;

    private User user = new User();
    
    private UserService userService;

    @Resource
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String update() {
        user = userService.load(user.getId());
        System.out.println(user);
        return INPUT;
    }
    
    @Override
    public User getModel() {
        return getUser();
    }
}



这个是我的实体类
Java code

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
    
    private Integer id;
    private String firstname;
    private String secondname;
    private int age;
    
    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getSecondname() {
        return secondname;
    }
    public void setSecondname(String secondname) {
        this.secondname = secondname;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}




这个是我的数据传输类(其实和上面的User一模一样的)
Java code

public class UserInfo {
    
    private Integer id;
    private String firstname;
    priv