java Struts2 - 从 JSP 访问 Action 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29429842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:15:41  来源:igfitidea点击:

Struts2 - Access from JSP to Action variables

javajspvariablesstruts2action

提问by iammg

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

我的Struts2有点问题,不知道为什么不能用...

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

我想通过 Action 类在两个 JSP 之间传递 2 个变量:

view1.jsp :

视图1.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

-> var1 和 var2 是我想传递给 Action 类的变量

struts.xml:

struts.xml:

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

Action.java :

动作.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)

-> 这工作正常;如果我将“System.out.print”与 getter 放在一起,它会向我展示 var1 (content1) 和 var2 (content2) 的良好值

view2.jsp :

视图2.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"

-> 这里有一个问题:我无法获取 var1 和 var2 的内容!
-><s:property value="var1" /><s:textfield value="%{var1}返回“null”

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

我的错误在哪里?没看懂。。。我是跟着网上别人说的。。。

Thank you !

谢谢 !

采纳答案by iammg

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

我终于找到了我的问题的答案!
要获得的价值var1var2我不得不使用那些下面几行:

view1.jsp :

视图1.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:

struts.xml:

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

Action.java :

动作.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 :

视图2.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
%>