论坛首页 Java企业应用论坛

提问:webwork初始化action的问题

浏览 2548 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-09-04  
初学webwork下来最新的webwork2.2.3,试了下starter这个例子发现个问题我把starter作了一点改动.在hellomatrix.jsp中原来是提交到HelloMatrixAction中的hello属性,我改成counterBean.userName
           <ww:form name="helloWorldForm" action="helloMatrixSubmit">
                    <ww:textfield name="counterBean.userName" label="Enter your name" />

public class CounterBean {
    private static int count = 0;
    private String userName;
    public int getCount(); {
        return count;
    }
    public void increment(); {
        count++;
    }
   public String getUserName(); {
       return userName;
	}
    public void setUserName(String userName); {
          this.userName = userName;
    } 
    
}

HelloMatrixAction
public class HelloMatrixAction extends ActionSupport {
	/** Spring managed bean reference */
	private CounterBean counterBean;

	/**
	 * IoC setter for the spring managed CounterBean.
	 * 
	 * @param counterBean
	 */
	public void setCounterBean(CounterBean counterBean); {
		this.counterBean = counterBean;
	}

	private String hello;

	private String message;

	public String getHello(); {
		return hello;
	}

	public void setHello(String hello); {
		this.hello = hello;
	}

	public String getMessage(); {
		return message;
	}

	public int getCount(); {
		return counterBean.getCount();;
	}

	/**
	 * A default implementation that does nothing an returns "success".
	 * 
	 * @return {@link #SUCCESS}
	 */
	public String execute(); throws Exception {
		System.out.println(this.getHello(););;
		return SUCCESS;
	}

	/**
	 * Sample sayHello method.
	 * 
	 * @return {@link #SUCCESS}
	 */
	public String sayHello(); throws Exception {
		ActionContext ctx = ActionContext.getContext();;
		Map map=ctx.getParameters();;
		
		String[] strs=(String[]);map.get("hello");;
		System.out.println(counterBean.getUserName(););;
		System.out.println(this.getHello(););;
		message = "users already took the red pill...";
		counterBean.increment();;
		return "he";
	}
}

在xwork.xml中
        <action name="helloMatrix" class="com.foo.example.HelloMatrixAction">
			   <result name="success">/WEB-INF/pages/hellomatrix.jsp</result>
			  <param name="hello">kkk</param> 
        </action>
        <action name="helloMatrixSubmit" class="com.foo.example.HelloMatrixAction" method="sayHello">
              <!--<interceptor-ref name="validationWorkflowStack"/>-->
			  <param name="hello">eee</param> 
	          <interceptor-ref name="params" />
	          <interceptor-ref name="model-driven" />
            <result name="input">/WEB-INF/pages/hellomatrix.jsp</result>
            <result name="error">/WEB-INF/pages/hellomatrix.jsp</result>
			<result name="he" type="dispatcher">
				  <param name="location">/WEB-INF/pages/hellomatrix.jsp</param>
			</result>
            <result name="success" type="freemarker">/WEB-INF/pages/hellomatrix-success.ftl</result>
        </action>

helloMatrix和helloMatrixSubmit是同一个类,但在走helloMatrix时hello被赋上初值“kkk”但当提交表单时走的是helloMatrixSubmit这时hello的值为null不知道是为什么。
   发表时间:2006-09-04  
getCounterBean
0 请登录后投票
   发表时间:2006-09-04  
我的意思是  我通过&lt;param name="hello"&gt;eee&lt;/param&gt;
给hello赋初值不好用
0 请登录后投票
   发表时间:2006-09-04  
interceptor 配置问题

第一个使用了默认的拦截器
第二个自己配置的,没有包含设置静态参数的拦截器
0 请登录后投票
   发表时间:2006-09-04  
谢谢,加了个&lt;interceptor-ref name="static-params"/&gt;好用了
0 请登录后投票
   发表时间:2006-09-04  
因为你在&lt;action name="helloMatrixSubmit" class="com.foo.example.HelloMatrixAction" method="sayHello"&gt;
              &lt;!--&lt;interceptor-ref name="validationWorkflowStack"/&gt;--&gt;
                          &lt;param name="hello"&gt;eee&lt;/param&gt;
                  &lt;interceptor-ref name="params" /&gt;
                  &lt;interceptor-ref name="model-driven" /&gt;
这里声明了interceptor,这样默认的 interceptor stack的default就失效了。
而default里面包含了static params的interceptor。

也就是说,你的问题其实是interceptor设置引起的。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics