开发者

I found a problem with ModelDriven of Struts2

开发者 https://www.devze.com 2023-01-12 12:24 出处:网络
Please download this and run it. I think it\'s worth trying though it\'ll be a little annoying. In execute method of action class, if you set the bean by the method returning beans, Modeldriven won\'

Please download this and run it. I think it's worth trying though it'll be a little annoying.

In execute method of action class, if you set the bean by the method returning beans, Modeldriven won't work in JSP( <s:property "someField" />won't work so you have to type the bean instance name like this; <s:property "myBean.someField" />).

But if you set the bean's field value, Modeldriven will work.

I know you may not believe me and think there's something else that I did wrong. But it's not true! Just try it..

public class DefaultClass extends ActionSupport implements ModelDriven<TestBean>    {
        TestBean test = new TestBean();
        DAO db = DAO.getInstance();

        public String access() throws Exception {
            //Beans setter doesn't work!
            setTest( db.getTest() );

            return SUCCESS;
        }
        public String access2() throws Exception    {
            //Field setter works! 
            test.setA(db.getA());
            test.setB(db.getB());

            return SUCCESS;
        }

I didn't know what I just found. So I ask this question. And as my own answer, if I p开发者_运维技巧ush the bean to the valuestack Modeldriven will work.

I wonder why there's no question about this. I think it's a serious problem.


I didn't know what I just found. So I ask this question.

You asked the question but ignored the answer that was given to you... The model driven interceptor is positioned after the prepare interceptor but long before the actual execution of the action. This means, if you want to retrieve an instance of your model class, you have to do the work in a "prepare" method. When getModel() is called on your action, it's going to be a reference to the TestBean before any of the logic in either of your action methods. In your other question that you linked, you mentioned that you expected the order to be different because you read about it in a book, which book was it? It wasn't Struts 2 In Action from Manning because in that one, they explicitly state (I think it's page 65) -

We should note one pitfall to avoid. By the time the execute() method of your ModelDriven action has been invoked, the framework has obtained a reference to your model object, which it’ll use throughout the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消