struts2将U​​RL传递给Action [英] struts2 Pass URL to Action

查看:77
本文介绍了struts2将U​​RL传递给Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通配符映射上阅读了struts手册并决定了为自己测试一些例子。我有一个动作指向:

 < action name =**method =getPersonclass =PersonActionBean > 
< result> /person/view.jsp< / result>
< / action>

这允许我去 / person 据我所知,看到 view.jsp 。所以我现在要做的是去 / person / jack / black 然后我想要 getPerson 方法在 PersonActionBean 类中获取URL字段 jack black 然后通过名称和姓氏在我的数据库中搜索然后填充将在 view.jsp上使用的对象



我关心的不是搜索功能,而是围绕从方法 getPerson 中检索URL中的字段。如何从URL中检索 jack black 并在我的 getPerson中使用方法?



我正在使用 struts 2.1.8.1

解决方案

方法1 - 使用struts2 -convention插件


struts.xml




 < constant name =struts.patternMatchervalue =namedVariable/> 




PersonAction.java




  import org.apache.struts2.convention.annotation.Namespace; 
...
@Namespace {/ persons / {param1} / {param2});
公共类PersonActionBean释放ActionSupport {
private String param1;
private String param2;
// getter and setter
}

如果你打电话给人/杰克/黑色,参数应设置为 param1 = jack param2 = black



方法2 - 没有struts2-convention插件


PersonAction.java




 公共类PersonActionBean释放ActionSupport {
private String param1;
private String param2;
// getter and setter
}




person.xml




 < package name =personnamespace =/ personextends = 网站 > 
< action name =* / *method =getPersonclass =PersonActionBean>
< param name =param1> {1}< / param>
< param name =param2> {2}< / param>
< result> /person/view.jsp< / result>
< / action>
< / package>




struts.xml




 < package name =websitenamespace =/extends =struts-default,json-default> 
...
< constant name =struts.enable.SlashesInActionNamesvalue =true/>
< constant name =struts.mapper.alwaysSelectFullNamespacevalue =false/>
...
< / package>

参考



查看高级通配符


I read the struts manual on wildcard mappings and decided to test some of the examples for myself. I have an action that points to:

<action name="**" method="getPerson" class="PersonActionBean">
      <result>/person/view.jsp</result>
</action>

This allows me to go anywhere past /person and see the view.jsp as far as I can understand it. So what I'm trying to do now is go to /person/jack/black then I want the getPerson method inside PersonActionBean class to get the URL fields jack and black and do a search in my DB by name and surname then populate an object that will be used on view.jsp

My concern isn't around the search functionality but around retrieving the fields in the URL from the method getPerson. How would I retrieve jack and black from the URL and use it in my getPerson method?

I'm using struts 2.1.8.1

解决方案

Method 1 - With struts2-convention plugin

struts.xml

<constant name="struts.patternMatcher" value="namedVariable"/>

PersonAction.java

    import org.apache.struts2.convention.annotation.Namespace;
    ...
    @Namespace{"/persons/{param1}/{param2}");
    public class PersonActionBean exends ActionSupport {
        private String param1;
        private String param2;
        // getter and setter
    }

If you call persons/jack/black, the params should be set to param1 = jack, param2 = black

Method 2 - Without struts2-convention plugin

PersonAction.java

public class PersonActionBean exends ActionSupport {
    private String param1;
    private String param2;
    // getter and setter
}

person.xml

<package name="person" namespace="/person" extends="website">
    <action name="*/*" method="getPerson" class="PersonActionBean">
            <param name="param1">{1}</param>
            <param name="param2">{2}</param>
            <result>/person/view.jsp</result>
    </action>   
</package>

struts.xml

<package name="website" namespace="/" extends="struts-default, json-default">
     ...
    <constant name="struts.enable.SlashesInActionNames" value="true"/>
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
     ...
</package>

References

Check out Advanced Wildcard

这篇关于struts2将U​​RL传递给Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆