You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Raghuveer.V" <ra...@infotechsw.com> on 2009/09/08 14:49:05 UTC

RE: Struts2 - ScopedModelDriven - Unable to update the model and set the latest Model data in session

This is working after using correct jar file, "struts2-core-2.1.6" jar.

 

  _____  

From: Raghuveer.V [mailto:raghuveerv@infotechsw.com] 
Sent: Wednesday, August 19, 2009 6:55 PM
To: 'user@struts.apache.org'
Subject: Struts2 - ScopedModelDriven - Unable to update the model and set
the latest Model data in session 

 

Hi,

 

I have a problem implementing ScopedModelDriven.

 

 

I Have Model / User Java Bean object in action class.

 

I am trying to implement concept of same model object being used for 3 JSP
pages with PREVIOUS and NEXT button navigation.

Data to be updated in Model object for every page navigation and to be saved
in session.

It is found the latest data is found from value stack when jsp page is
navigated to next page, but not updated in Model object either in request or
session scope 

I am sure I have configured necessary interceptors properly.

 

This could be easily done in struts1 by setting the Actionform in session
scope.

Any advice or thought?.

 

 

Struts.xml:

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

<package name="myPackage" namespace="/" extends="struts-default">

<interceptors>

        <interceptor name="myMultipleForm"

 
class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor">

         <param name="scope">session</param>

         <param name="name">user</param>

          <param name="className">com.ut.p.s2.beans.User</param>         

       </interceptor>

</interceptors>

 

<global-results>

    <result name="error">/jsp/Error.jsp</result>

    <result name="invalid.token">/jsp/Error.jsp</result>

    <result name="login">/jsp/login.jsp</result>

</global-results>

 

<global-exception-mappings>

    <exception-mapping

            result="error"

            exception="java.lang.Throwable"/>

</global-exception-mappings>

 

<action name="ShowScopedModel" class="com.ut.p.s2.actions.ServletTestAction"
>

 <interceptor-ref name="servletConfig"/>

<interceptor-ref name="app_common"/>

<result name="model_test_page">/jsp/smodelTest.jsp</result>

<result>/jsp/smodelTest.jsp</result>

</action>

<action name="scopedModelAction_*"
class="com.ut.p.s2.actions.ScopedModelDrivenAction" method="{1}">

            <interceptor-ref name="basicStack"/>

            <interceptor-ref name="defaultStack" />

            <interceptor-ref name="prepare"/>

            <interceptor-ref name="debugging"/>

            <interceptor-ref name="scopedModelDriven">

            <param name="scope">session</param>

                        <param name="name">user</param>

                        <param
name="className">com.ut.p.s2.beans.User</param>    

            </interceptor-ref>

            <interceptor-ref name="params"/>

            <interceptor-ref name="conversionError"/>

            <interceptor-ref name="workflow"/>

            <result name="input">/jsp/smodelTest.jsp</result>

            <result>/jsp/smodelResult.jsp</result>

            <result name="model_test_page">/jsp/smodelTest.jsp</result>

            <result name="model_test_page2">/jsp/smodelTest2.jsp</result>

            <result name="model_test_page3">/jsp/smodelTest3.jsp</result> 

            <result name="model_result_page">/jsp/smodelResult.jsp</result> 

</action>

</package>

</struts>

 

 

 

Action:

 

package com.ut.p.s2.actions;

import java.util.ArrayList;

import com.ut.p.s2.beans.Books;

import com.ut.p.s2.beans.Dept;

import com.ut.p.s2.beans.User;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.Preparable;

import com.opensymphony.xwork2.interceptor.ScopedModelDriven; 

 

public class ScopedModelDrivenAction extends ActionSupport implements
ScopedModelDriven,Preparable {

 

            private User user =null;

            private static final long serialVersionUID =
1271130427666936592L;

        private String scope = null;

 

            public void prepare() throws Exception {

            user = new User();

            ArrayList arlList=new ArrayList();

            Books b1 = new Books("Java",100);

            Books b2 = new Books("VB",200);

            arlList.add(b1);

            arlList.add(b2);

            

            Dept dept = new Dept();

            dept.setDeptNo("100");

            dept.setDeptName("Mechanical");

            user.setDept(dept);

            user.setArlList(arlList);

            }

 

            public String getScopeKey() {

                        return scope;

            }

 

            public void setModel(Object arg0) {

                        this.user = (User) arg0;

            }

 

            public void setScopeKey(String arg0) {

                         scope = arg0;

            }

            public Object getModel() {

                         return user;

            }

 

            /**

             * @return the user

             */

            public User getUser() {

                        return user;

            }

 

            /**

             * @param user the user to set

             */

            public void setUser(User user) {

                        this.user = user;

            }

            

            

            public String execute() {

            System.out.println("execute()........."+user);

            return INPUT;

    }

    

    public String input() throws Exception {

            System.out.println("input()........."+user);

        return SUCCESS;

    }

    

    public String save() {

            System.out.println("save()........."+user);

            return "model_result_page";

    }

    

    public String page1() {

            System.out.println("page1()........."+user);

            System.out.println("page1()....scope....."+scope);

            return "model_test_page";

    }

    

    public String page2() {

            System.out.println("page2()........."+user);

            System.out.println("page2()....scope....."+scope);

            return "model_test_page2";

    }

 

    public String page3() {

            System.out.println("page3()........."+user);

            System.out.println("page3()....scope....."+scope);

            return "model_test_page3";

    }

 

}//end class

 

Java Beans:

 

package com.ut.p.s2.beans;

 

import java.util.ArrayList;

 

public class User {

 

    private String name;

    private int age;

    private String sex;

    private String[] hobby;

    private String country;

    private ArrayList arlList;

    

    private Dept dept=new Dept();

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    public String getSex() {

        return sex;

    }

 

    public void setSex(String sex) {

        this.sex = sex;

    }

 

    public String[] getHobby() {

        return hobby;

    }

 

    public void setHobby(String[] hobby) {

        this.hobby = hobby;

    }

 

    public String getCountry() {

        return country;

    }

 

    public void setCountry(String country) {

        this.country = country;

    }

 

            /**

             * @return the arlList

             */

            public ArrayList getArlList() {

                        return arlList;

            }

 

            /**

             * @param arlList the arlList to set

             */

            public void setArlList(ArrayList arlList) {

                        this.arlList = arlList;

            }

 

 

            /**

             * @return the dept

             */

            public Dept getDept() {

                        return dept;

            }

 

            /**

             * @param dept the dept to set

             */

            public void setDept(Dept dept) {

                        this.dept = dept;

            }

            

            public String toString() {

        return "User{" +

                "name=" + name +

                ", age='" + age + '\'' +

                ", arlList='" + arlList + '\'' +

                

                "dept=" + dept +

                '}';

    }

            

}//end class User

 

package com.ut.p.s2.beans;

 

import com.ut.p.s2.base.CAppBean;

 

public class Dept extends CAppBean{

            

            private String deptNo = null;

            private String deptName = null;

            /**

             * @return the deptName

             */

            public String getDeptName() {

                        return deptName;

            }

            /**

             * @param deptName the deptName to set

             */

            public void setDeptName(String deptName) {

                        this.deptName = deptName;

            }

            /**

             * @return the deptNo

             */

            public String getDeptNo() {

                        return deptNo;

            }

            /**

             * @param deptNo the deptNo to set

             */

            public void setDeptNo(String deptNo) {

                        this.deptNo = deptNo;

            }

            

            

            

            public String toString() {

        return "Dept{" +

                "deptNo=" + deptNo +

                ", deptName='" + deptName + '\'' +

       

                '}';

    }

 

}//end class Dept

 

package com.ut.p.s2.beans;

 

import java.util.ArrayList;

 

 

public class Books {

             private String name;

             private int price;

             

             public Books(String name,int price) {

 

                    this.name = name;

                    this.price = price;

                }

            /**

             * @return the name

             */

            public String getName() {

                        return name;

            }

            /**

             * @param name the name to set

             */

            public void setName(String name) {

                        this.name = name;

            }

            /**

             * @return the price

             */

            public int getPrice() {

                        return price;

            }

            /**

             * @param price the price to set

             */

            public void setPrice(int price) {

                        this.price = price;

            }

            

            public String toString() {

        return "Books{" +

                "name=" + name +

                ", price='" + price + '\'' +

               

                '}';

    }

}//end class Book

 

 

JSP Page:

 

<%@ page errorPage="/jsp/Error.jsp" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="/struts-tags" prefix="s" %>

<html>

<head>

<s:head theme="simple"/>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>User Details</title>

</head>

<body>

    <h2>User Details</h2>

    <hr>

    User Name :<s:property value="name" /><br>

    Age :<s:property value="age" /><br>

    Hobbies :<s:property value="hobby" /><br>

    Country :<s:property value="country" /><br>

 

<TABLE border="1">

 <s:iterator value="arlList" status="row">

<TR>

 <TD><s:textfield name="arlList[%{#row.index}].name"  value="%{name}"
/></TD>

 <TD> <s:text name="arlList[%{#row.index}].price"   /></TD>

</TR>

</s:iterator>

</TABLE>

<h2>

User Department :<s:property value="dept" /><br>

<s:property value="dept.id" />

<s:property value="dept.name" />

<jsp:include page="Footer.jsp"/>

</body>

</html>