简体   繁体   English

如何将对象从JSP表单传递到Struts2中的Action类而不将所有表单字段定义为类属性?

[英]How to pass an object from a JSP form to an Action class in Struts2 without defining all form fields as a class property?

To make the question clear I make an example: 为了使问题清楚,我举了一个例子:

  1. consider theSubscriber form defined bellow in first.jsp : 考虑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> 
  2. consider the following action class for the next.action 考虑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; } } 
  3. The question is: I know that defining all the form fields as action class properties let the class fill them correctly. 问题是:我知道将所有表单字段定义为操作类属性可让类正确填充它们。 But I want to make it fill the relevant fields in another class which contains all necessary properties. 但我想让它填充另一个包含所有必要属性的类中的相关字段。 The user/subscriber class is like this: 用户/订阅者类是这样的:

     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; } 

I have defined all accessor methods. 我已经定义了所有访问器方法。 In fact the problem is that it looks very annoying and redundant to define all these fields for the nextAction class and then evaluate the instance of user in that class. 事实上,问题是为nextAction类定义所有这些字段然后评估该类中的用户实例看起来非常烦人和多余。

How should I solve this problem? 我该如何解决这个问题?

It is easier than you think. 这比你想象的要容易。

You are very close, and you've already done it for value attribute (used to pre-set the value for the item on the page): now just do it for name attribute (used to send the value to the Action), like this: 你非常接近,你已经完成了value属性(用于预先设置页面上项目的值):现在只需为name属性(用于将值发送到Action),就像这个:

<s:hidden name="user.id" key="id" value="%{user.id}"  />
<s:textfield id="subscriberForename" name="user.forename"
    value="%{user.forename}" label="Forename" />
<s:textfield id="subscriberSurname" name="user.surname" 
    value="%{user.surname}" label="Surname" />

( Key attribute was not necessary here) (此处不需要Key属性)

Note that for this to work, you will need a bean with a no-args constructor (as you have). 请注意,要使其工作,您将需要一个带有无参数构造函数的bean(就像您一样)。

Avoid initializating it yourself too. 避免自己初始化。

Change this: 改变这个:

private Subscriber user = new Subscriber();

to this 对此

private Subscriber user;

I think you are asking how you can persist in memory a User object that will be used across multiple actions. 我想你是在问如何在内存中保留一个将在多个动作中使用的User对象。

You have a couple of options if I understand your question correctly. 如果我正确理解您的问题,您有几个选择。

One option, which is not very Struts like, would be to add the User object to a session variable. 一个不太像Struts的选项是将User对象添加到会话变量中。 You can access the session variable as described here . 您可以按此处所述访问会话变量。

Another option, which I would recommend if you plan on maintaining the object in your session for a specific set of actions (or steps, such as a 'wizard' portion of your app) is the ScopeInterceptor. 另一个选项,如果您计划在会话中为特定的一组操作(或步骤,例如应用程序的“向导”部分)维护对象,我建议使用ScopeInterceptor。 You can read about that here and here . 你可以在这里这里阅读。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM