Struts 2上的ModelDriven [英] ModelDriven on struts 2

查看:179
本文介绍了Struts 2上的ModelDriven的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用hibernate,struts2和spring开发一个项目,但我的问题是struts。

我创建了3个扩展ActionSupport的类并正在实现modeldriven for所有人都是同一个班级。我已经实现了一些在调用动作时要执行的方法。

类的结构类似于(Class1Action.java):


I am developing a project using hibernate, struts2 and spring, but my problems are with struts.
I have created 3 classes extending ActionSupport and are implementing modeldriven for the same class in all of them. I have implemented some methods to be executed when the actions are called.
The structure for the classes is like (Class1Action.java):

public class Class1Action extends ActionSupport implements ModelDriven<ModelDrivenClass> {

     private ModelDrivenClass modelDrivenClass;


     // getter and setter for modelDrivenClass

     public String methodName() {

     System.out.println("Entrou!");

        return SUCCESS;

    }

    @Override
    public Sensor getModel() {
        return getSensor();
    }

}

在我创建的struts.xml上3使用下一个结构的动作(struts.xml):

On struts.xml I have created 3 action using the next structure (struts.xml):

<action name="actionName1" method="methodName" class="Class1Action">
   <interceptor-ref name="validation">
      <param name="excludeMethods">methodName</param>
   </interceptor-ref>
   <result name="success" >success.jsp</result>
   <result name="input" >input.jsp</result>
</action>

除了我有一个带3个按钮的JSP引用不同的struts动作,还有几个代表字段的字段来自3个动作类中使用的模型驱动类,以及< s:push> 标记内的所有字段。
问题是当我填充字段时,只需点击任何按钮后,字段中的数据就会丢失。
我试图删除从验证中排除方法的3个struts行,但是不是字段是空的,第二次我按下相同的按钮时他返回一个输入并重定向到input.jsp。

下一个代码来自success.jsp,即起始页面:

Besides that I have a JSP with 3 buttons referring the different struts actions, and several fields that represents fields from the model driven class used in the 3 action classes, and all of the fields inside a <s:push> tag. The problem is when I am populating the fields and just after click on any of the buttons, data in the fields are missing. I have tried to remove the 3 struts lines that excludes the methods from validation, but instead of the fields are being empty, at the second time I have pressed the same button he returns an input and redirects to input.jsp.
The next code is from success.jsp, that is the starting page:

<form method="post" >


    <s:push value="modelDrivenClass">

        <s:textfield label="FieldLabel1" name="modelDrivenClassAttribute1" />
        <s:textfield label="FieldLabel2" name="modelDrivenClassAttribute2" />
        <s:textfield label="FieldLabel3" name="modelDrivenClassAttribute3" />
        <s:textfield label="FieldLabel4" name="modelDrivenClassAttribute4" />
        <s:textfield label="FieldLabel5" name="modelDrivenClassAttribute5" />

    </s:push>


<s:submit action="actionName1" name="Submit1" value="Submit1" />
<s:submit action="actionName2" name="Submit2" value="Submit2" />
<s:submit action="actionName3" name="Submit3" value="Submit3" />
</form>

我不知道这是否是正确的方法,但是我做到了我只使用了一个实现modeldriven的类,这个类有3个方法。我只是尝试这种方式,因为我想让我的代码清楚,并且只在一个类中没有所有方法。

I don't know if is this the right way to do it, but I made it work when I used only one class implementing modeldriven and this class has all the 3 methods. I am just trying this way because I would like to let my code clear and don't have all methods in only one class.

推荐答案

<如果要将旧的Struts代码迁移到新的代码,那么使用模型驱动的动作类的方法非常有用,因此它简化了表单bean的概念。并且在较新的Struts 2中没有必要使用 ModelDriven 如果你可以使用 top 中的action bean与其名称前缀相关联的值堆栈和模型。

An approach with model driven action class is very useful if you are migrating old Struts code to a new one, so it simplifies a concept of form bean. And in the newer Struts 2 isn't necessary to use ModelDriven if you can use the action bean from the top of the value stack and model associated within just prefixed to its name.

注意,在操作配置中覆盖拦截器配置时 defaultStack 消失。因此,最好至少创建自己的堆栈或引用 basicStack ,以确保调用必要的拦截器。在你的情况下一个 modelDriven 拦截器。

Note, that when overriding interceptors configuration in the action config the defaultStack disappears. So, better to create your own stack or reference at least basicStack to make sure necessary interceptors are invoked. In your case a modelDriven interceptor.

如果在堆栈上使用此拦截器,则会在操作前推送模型,因此您不需要 s:push 结果中的它。

If you use this interceptor on the stack it pushes the model in front of the action, so you don't need to s:push it in the result.

模型对象应初始化为模型类的实例,并由模型的getter返回。还要考虑在模型的验证字段时使用访问者验证程序。

The model object should be initialized to the instance of he model class and returned by the getter of the model. Also consider using a visitor validator when validation fields of a model.

在调用之间共享数据的三个操作需要使用 可预删除 以填充会话中的字段或使用会话对象引用为字段提供默认值以保存它们。

Having three actions that share a data between calls requires preparing a model using Preparable to populate fields from session or use a session object reference to provide default values for fields to keep them saved.

注意,使用 prepare 需要更改拦截器调用的顺序来推送模型在它填充之前。

Note, using prepare requires changing the order of interceptors call to push the model before it's populated.

这篇关于Struts 2上的ModelDriven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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