简体   繁体   English

如何在操作中使用别名拦截器获取实用程序类对象的属性

[英]How to use the alias interceptor for the properties of utility class object in action

I have an Action class named MyFirstClass in which I have a String variable and a User variable as follows:我有一个名为MyFirstClassAction类,其中有一个String变量和一个User变量,如下所示:

public class MyFirstClass extends ActionSupport implements ModelDriven<User>,Preparable {

    User user;
    
    private String nickName;

    public void prepare(){
       user = new User();       
    }

    public User getModel(){
       return user;
    }
    .........................
    ........................    
}

The User class has String variables as userName and userAge . User类具有String变量作为userNameuserAge

As my Action class has implemented ModelDriven interface, the variables of the User class are supposed to get/set on the request.由于我的Action类已经实现了ModelDriven接口, User类的变量应该在请求上获取/设置。

I have a JSP file also which is designed as follows:我还有一个 JSP 文件,其设计如下:

<s:form action="index">
<s:actionerror/>
<s:textfield name="myname" label="UserName:">
</s:textfield>
<s:textfield name="myage" label="UserAge:">
</s:textfield>
<s:submit key="submit" name="submit"/>
</s:form>

and struts.xml is designed as:并且struts.xml被设计为:

<package name="default" namespace="/" extends="struts-default">
        <action name="index" class="com.actionClasses.MyFirstClass">
            <param name="aliases">#{'myname':'nickName','myname':user.userName,'myage':user.age}</param>
            <interceptor-ref name="alias"/> 
            <interceptor-ref name="basicStack"/>        
            <result name="success">/success.jsp</result>
            <result name="input">/user.jsp</result>                
        </action>
</package>

My problem is:我的问题是:

Since my JSP page text fields' name do not match with the property names in the User class.由于我的 JSP 页面文本字段的名称与User类中的属性名称不匹配。 I am not able to set the request parameters, like this, to the corresponding properties in the action with the help of alias interceptor.alias拦截器的帮助下,我无法像这样将请求参数设置为操作中的相应属性。

Change the configuration for aliases to match property names, also use defaultStack you need it because it contains modelDriven interceptor .更改别名的配置以匹配属性名称,还可以使用您需要的defaultStack ,因为它包含modelDriven拦截器

You have stated你已经声明

User class has String variables as userName and userAge . User类具有字符串变量作为userNameuserAge

If the form is like如果表格是这样的

user.jsp:用户.jsp:

<s:form action="index">
<s:actionerror/>
<s:textfield name="name" label="UserName:">
</s:textfield>
<s:textfield name="age" label="UserAge:">
</s:textfield>
<s:submit key="submit" name="submit"/>
</s:form>

and result is结果是

success.jsp:成功.jsp:

<s:actionerror/>
<s:label name="userName" label="UserName:"/><br/>
<s:label name="userAge" label="UserAge:"/><br/>

the configuration should be:配置应该是:

<package name="default" namespace="/" extends="struts-default">
    <action name="index" class="com.actionClasses.MyFirstClass">
        <param name="aliases">#{'name':'userName','age':'userAge'}</param>
        <interceptor-ref name="defaultStack"/>        
        <interceptor-ref name="alias"/> 
        <result name="success">/success.jsp</result>
        <result name="input">/user.jsp</result>                
    </action>
</package>

The alias interceptor is at the end because it should be after modelDriven . alias拦截器在最后,因为它应该在modelDriven之后。

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

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