0

我有一个要求,在没有会话的情况下将参数从一个操作传递到另一个操作:这是我的代码,我正在尝试使用范围拦截器。这是我的代码我不确定我做错了什么,但我无法得到结果。

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

<struts>
    <package name="default" extends="struts-default">
        <action name="HelloWorld" class="com.tutorials4u.helloworld.HelloWorld">
            <result name="SUCCESS">/success.jsp</result>
        </action>

         <action name="FirstAction" class="com.tutorials4u.helloworld.FirstAction">
            <interceptor-ref name="basicStack" />   
    <interceptor-ref name="scope">
        <param name="session">myName</param>
        <param name="key">person</param>
        <param name="type">start</param>
    </interceptor-ref>      

            <result name="SUCCESS">/success1.jsp</result>
        </action>

         <action name="SecondAction" class="com.tutorials4u.helloworld.SecondAction">
           <interceptor-ref name="scope">
        <param name="session">myName</param>
        <param name="key">person</param>
        </interceptor-ref>
        <interceptor-ref name="basicStack" />  


            <result name="SUCCESS">/success2.jsp</result>
        </action>
    </package>
</struts>


*index.jsp*
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <s:form action="HelloWorld" >
            <s:textfield name="userName" label="User Name" />
            <s:submit />
        </s:form>
    </body>
</html>

*Success.jsp*
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
    <s:property value="person.myName"></s:property>
    <s:form action="FirstAction" >




        <s:submit value="Click for first Action"></s:submit>
        </s:form>
    </body>
</html>

*success1.jsp*

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
    <s:property value="person.myName"></s:property>
        <h1><s:property value="message" /></h1>I am in FIRST ACTION


        <s:form action="SecondAction">
        <s:submit value="click for 2nd action"></s:submit></s:form>
    </body>
</html>

*success2.jsp*
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <h1><s:property value="message" /></h1>2ND ACTION EXECUTED
       <s:property value="person.myName"></s:property>
    </body>
</html>
**ACTIONS**
package com.tutorials4u.helloworld;

import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * 
 */

public class HelloWorld extends ActionSupport{

    private static final long serialVersionUID = 1L;

    private String message;

    private String userName;


    public HelloWorld() {
    }

    public String execute() {
        setMessage("Hello " + getUserName());
        return "SUCCESS";
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

}
*FirstAction.java*
package com.tutorials4u.helloworld;

import com.opensymphony.xwork2.ActionSupport;

public class FirstAction extends ActionSupport {

    /**
     * 
     */

    private String myString;
    private Person person;
    private static final long serialVersionUID = 1L;
     public FirstAction(){

     }
     public String execute() {

            return "SUCCESS";
        }
    public String getMyString() {
        return myString;
    }
    public void setMyString(String myString1) {

        this.myString = myString1;
    }
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }


}


package com.tutorials4u.helloworld;

    public class Person {

        private String myName;

        public String getMyName() {
            return myName;
        }

        public void setMyName(String myName1) {
            myName1 = "test string";
            this.myName = myName1;
        }
    }

SecondAction.java

    package com.tutorials4u.helloworld;

    import com.opensymphony.xwork2.ActionSupport;

    public class SecondAction extends ActionSupport  {

        private static final long serialVersionUID = 1L;
         private Person person;
        public SecondAction(){

        }
         public String execute() {

                return "SUCCESS";
            }
        public Person getPerson() {
            return person;
        }
        public void setPerson(Person person) {
            this.person = person;
        }
    }

---I would really appreciate if you can help me on where I was doing things wrong..
TIA
4

1 回答 1

0

所以我们在每个其他动作中都实例化会话对象,对吗?

根本不是,我们只是从 ServletActionContext 中检索它。

如何进入会话

ServletActionContext.getContext().getSession().put("softuser", someUserObject);

如何从会话中检索

ServletActionContext.getContext().getSession().get("softuser");
于 2013-11-07T03:31:34.917 回答