如何在Struts 2的单个视图中使用多个表单/动作 [英] How to use multiple forms / actions in a single view in Struts 2

查看:124
本文介绍了如何在Struts 2的单个视图中使用多个表单/动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索框,该搜索框显示在每个页面上.搜索框的JSP代码通过图块插入到每个页面中.

I have a search box which is displayed on every page. The JSP code for the search box is inserted into every page via tiles.

搜索框具有一个表单和一个动作类SearchAction,它需要为下拉框预加载一些属性. SearchAction类具有input()方法,该方法将进行初始化.

The search box has a form and an action class SearchAction which needs to preload some properties for drop down boxes. The SearchAction class has an input() method, which does this initialization.

某些页面在主要区域也有自己的形式.还带有自己的动作课.他们还具有input()方法,该方法可以进行一些预加载.

Some pages also have their own form in the main area. Also with their own action class. They also have an input() method which does some preloading.

  1. 是否可以在同一视图中使用两个动作?
  2. 每个表单如何访问其自身操作的属性.
  3. 在呈现JSP之前,我如何设法同时调用两个动作类的输入方法?

更新:

我正在添加一个精简示例,因为可能不清楚我想做什么.这是带有RegisterAction的寄存器页面register.jsp.该页面还包含搜索表单. (顺便说一句:(我省略了动作类中的getter/setter和其他内容,以使其简短):

I am adding a trimmed down example, since it's probably not clear what I am trying to do. It's a register page register.jsp with an RegisterAction. And the page also contains the search form. (BTW: I left out the getter/setter and other stuff in the action classes to keep it short):

register.jsp:

<s:form action="executeSearch">
    <s:textfield key="name" label="Name"/>
    <s:textfield key="age"  label="Age"/>
    <s:submit/>
</s:form>

<s:form action="executeRegister">
    <s:textfield key="firstName" label="First Name"/>
    <s:textfield key="lastName" label="Last Name"/>
    <s:textfield key="age" label="Age"/>
    <s:submit/>
</s:form>

struts.xml:

<action name="*Search" class="action.SearchAction" method="{1}">
    <result name="success">/searchresult.jsp</result>
</action>

<action name="*Register" class="action.RegisterAction" method="{1}">
    <result name="input">/register.jsp</result>
    <result name="success">/registerOk.jsp</result>
</action>

SearchAction.java:

public class SearchAction extends ActionSupport {

    private String name;
    private int age;

    @Override
    public String input() throws Exception {
        // preload the search form with some demo data
        name = "test";
        age = 20;
        return INPUT;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    ...
}

RegisterAction.java:

public class RegisterAction extends ActionSupport {

    private String firstName;
    private String lastName;
    private int age;

    @Override
    public String input() throws Exception {
        // preload the register form with some demo data
        firstName = "John";
        lastName = "Rambo";
        age = 10;
        return INPUT;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    ...
}

比方说我将动作称为inputRegister.action.然后调用RegisterAction.input().设置属性.结果SUCCESS导致register.jsp呈现.

Let's say I call the action inputRegister.action. Then RegisterAction.input() is called. The properties are set. And result SUCCESS causes register.jsp to be rendered.

那我的搜索表单呢?如何获得对搜索操作及其模型的访问权限.当然,这两个都不在ValueStack上.而且,我也没有找到一种方法来调用SearchAction的任何方法以初始化其模型.我故意选择age作为这两个动作类.在呈现的页面中,您可以看到搜索表单还访问了RegisterAction-属性(因为它位于ValueStack的顶部).但是它需要访问SearchAction.它必须显示20而不是10.

But what about my search form. How do I get access to the search action and it's model. Neither of those are on the ValueStack of course. And neither do I see a way to call any methods of SearchAction in order to initialize it's model. I deliberately choose age to be in both action classes. In the rendered page you can see that the search form also accesses the RegisterAction-properties (because that's on top of the ValueStack). But it needs to access SearchAction. It must display 20 and not 10.

我显然做错了.但是即使经过大量的搜索,我仍然没有找到正确的方法.

I am clearly doing it wrong. But even after a lot of googling, I still did't find out the right way to do it.

推荐答案

视图的概念用于执行任意数量的操作,并且任意操作可以具有任意数量的结果.结果用于返回视图.返回视图的动作位于ValueStack的顶部,可以通过评估OGNL表达式轻松访问.结果视图可以映射到任何动作类中的任何动作.它对调用它的操作一无所知,但是可以再次使用OGNL来确定它.您可以将动作映射到同一动作类的不同方法,因此不需要很多input()方法.

The concept of the view is used to perform any number of actions, and any action can have any number of results. A result is used to return a view. The action that returns a view is placed on top of the ValueStack to be easy accessible via evaluating OGNL expressions. The result view could be mapped to any action in any action class. It knows nothing about action which calls it, but it can determine it, again using OGNL. You can map actions to different method of the same action class, so you don't need to have many input() methods.

您的问题是表单字段的边界错误.每个表格应有一个单独的存储库以正确显示它.字段以名称为界,最好为搜索条件变量提供一个表单bean,并将其放在操作上下文中的某个位置.

You problem is you bounded form fields wrong. Each form should have a separate storage to display it correctly. Fields are bounded by name, and you'd better have a form bean for the search condition variables and put it somewhere in the action context.

<s:form action="executeSearch">
    <s:textfield key="searchBean.name" label="Name"/>
    <s:textfield key="searchbean.age"  label="Age"/>
    <s:submit/>
</s:form>

<s:form action="executeRegister">
    <s:textfield key="firstName" label="First Name"/>
    <s:textfield key="lastName" label="Last Name"/>
    <s:textfield key="age" label="Age"/>
    <s:submit/>
</s:form> 

注意:key属性生成namelabelvalue.

Note: the key attribute generates name, label, and value.

每个操作都需要初始化searchBean,除非它具有会话作用域.如果将bean放入会话中,则应使用#session.前缀.您可以在文档页面上找到有关 OGNL 的更多信息.

Each action needs to initialize a searchBean, unless it has a session scope. If you put a bean to a session, then you should use #session. prefix. More about OGNL you can find on the docs page.

这篇关于如何在Struts 2的单个视图中使用多个表单/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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