为了让问题更清楚,我举了一个例子:
考虑
theSubscriber
下面定义的形式first.jsp
:<s:form id="theSubscriber" name="theSubscriber" method="post" action="next.action"> <s:bean name="my.hibernate.actors.Subscriber" var="user"> <s:hidden name="id" key="id" value="%{user.id}" /> <s:textfield id="subscriberForename" name="forename" key="forename" value="%{user.forename}" label="Forename" /> <s:textfield id="subscriberSurname" name="surname" key="surname" value="%{user.surname}" label="Surname" /> </s:bean> </s:form>
为 next.action 考虑以下动作类
public class NextAction extends ActionSupport { private Subscriber user = new Subscriber(); private String forename; public String getForename() { return forename; } public void setForename(String forename) { this.forename = forename; } public ManageSubscriber() { // TODO Auto-generated constructor stub } public ManageSubscriber(Subscriber user) { this.user = user; } public Subscriber getUser() { return user; } public void setUser(Subscriber user) { this.user = user; } public String execute() { System.out.println(getUser());//This prints out null System.out.println(getForename());//This prints out the posted forename return SUCCESS; } }
问题是:我知道将所有表单字段定义为操作类属性可以让类正确填充它们。但我想让它填充另一个包含所有必要属性的类中的相关字段。用户/订阅者类是这样的:
public class User { private long id; private String username; private String password; private UserLevel userLevel; private String forename; private String surname; private String email; private String phoneNumber; private String address; private Date birthday; private Date registrationDate; }
我已经定义了所有访问器方法。事实上,问题是为nextAction
类定义所有这些字段然后评估该类中的用户实例看起来非常烦人和多余。
我应该如何解决这个问题?