0

I have got a little problem with Struts2, and I don't know why it doesn't work ...

I want to pass 2 variables between two JSP, via an Action class :

view1.jsp :

<s:form action="myAction">
   <input id="var1" name="var1" type="text" />
   <input id="var2" name="var2" type="text" />
   <button type="submit"> Ok </button>
</s:form>

-> var1 and var2 are the variables that I want to pass to the Action class

struts.xml:

<action name="myAction" class="MyAction" method="execute">
    <result name="success">view2.jsp</result>
</action>

Action.java :

public class MyAction extends DefaultActionSupport
{
    private String var1;
    private String var2;

public String execute() throws Exception
{
    // ... Some actions ...
    return SUCCESS;
}

// Getters & Setters for var1 and var2 (generated by Eclipse)
public String getVar1()
{
    return var1;
}

public void setVar1(String var1)
{
    this.var1 = var1;
}

public String getVar2()
{
    return var2;
}

public void setVar2(String var2)
{
    this.var2 = var2;
}

-> This works correctly ; if I put "System.out.print" with the getters, it shows me the good values of var1 (content1) and var2 (content2)

view2.jsp :

Values of var1 = <s:property value="var1" />
Values of var2 = <s:property value="var2" />

Textfield with var1 in default-value : <s:textfield value="%{var1}" />
Textfield with var2 in default-value : <s:textfield value="%{var2}" />

-> There is a problem here : I can't get the content of var1 and var2 !
-> <s:property value="var1" /> and <s:textfield value="%{var1} are returning "null"

Where is my error ? I don't understand ... I followed what the others said on the internet ...

Thank you !

iammg
  • 131
  • 1
  • 1
  • 5
  • Could you give us you getters and setters as they matter ? – yunandtidus Apr 03 '15 at 09:56
  • Hi ! Thank you for your comment I have edited my Message, with the content of the Getters and Setters – iammg Apr 03 '15 at 10:09
  • Have you put the code of view2.jsp in form tag. Like ... – Anshuman Apr 04 '15 at 18:39
  • is there any exception coming on the server console – Code2Interface Apr 05 '15 at 16:25
  • @Anshuman : In View2.jsp, I put it like this way : ` ` >>> It doesn't work either when it's put in a form tag ; I've got `null` value instead the real values @Code2Interface : I've got nothing in the server console ; The only things that I've got in the console are the associate result of : `System.out.println("`value of var1 = " + getVar1());` The values which are showing in the console are corrects ... But it "null" in the JSP – iammg Apr 13 '15 at 08:31

1 Answers1

0

I finally found the answer to my problem !
To get the value of var1and var2I had to use those following lines :

view1.jsp :

<s:form action="myAction">
   <input id="var1" name="var1" type="text" />
   <input id="var2" name="var2" type="text" />
   <button type="submit"> Ok </button>
</s:form>

struts.xml:

<action name="myAction" class="MyAction" method="execute">
    <result name="success">view2.jsp</result>
</action>

Action.java :

public class MyAction extends DefaultActionSupport{
    private String var1;
    private String var2;

    public String execute() throws Exception{
        // ... Some actions ...

        ActionContext.getContext().getSession().put("var1", getVar1());
        ActionContext.getContext().getSession().put("var2", getVar2());

        return SUCCESS;
    }

    // Getters & Setters for var1 and var2 (generated by Eclipse)
    public String getVar1(){
        return var1;
    }

    public void setVar1(String var1){
        this.var1 = var1;
    }

    public String getVar2(){
        return var2;
    }

    public void setVar2(String var2){
        this.var2 = var2;
    }
}

view2.jsp :

Values of var1 = <s:property value="#session.var1" />
Values of var2 = <s:property value="#session.var2" />

//To transform var1 and var2 into JSP variables :

<s:set var="var1 " value="#session.var1 " />
<s:set var="var2 " value="#session.var2" />
<jsp:useBean id="var1 " type="java.lang.String" />
<jsp:useBean id="var2 " type="java.lang.String" />

<%
    String myString1 = var1;
    String myString2 = var2
%>
iammg
  • 131
  • 1
  • 1
  • 5
  • 1
    Definitely not a solution. – Andrea Ligios Apr 16 '15 at 13:36
  • @AndreaLigios How can I do it better ? – iammg Apr 17 '15 at 08:16
  • Using getters and setters instead of using session, that is useless here. And when using session, BTW, use SessionAware (for example in your DefaultActionSupport), that is preferred to getting it from ActionContext. Read more: http://stackoverflow.com/a/19540712/1654265 – Andrea Ligios Apr 17 '15 at 08:18
  • If your Interceptor Stack is the default one, or it's correctly configured, and you can't get your attributes through setters automatically, you have probably some problem with the attribute's names and their case, for example if var1 and var2 are vAr1 and vAr2, Eclipse can't generate the getters and setters properly: http://stackoverflow.com/a/27928298/1654265 – Andrea Ligios Apr 17 '15 at 08:25