我根据书上的内容,写了个例子,一个表单发送一个文本框的值到一个action,这个action执行完后转到一个JSP页面,JSP中一个自定义标签调用action的方法。我的理解这个请求/响应的处理过程是这样:
发送index.jsp中文本框的值到helloworld.action,接着读取xwork.xml文件,然后执行HelloWorld这个类中的execute()方法(在执行方法之前就通过setXXX的一些方法设置了类变量的值),然后转到hello.jsp页面,执行自定义property标签对应action中的getmert方法,显示结果到浏览器。不知道我理解的有没有错?如果不是这样,那么这个处理过程是怎么样的?还有,执行execute()方法之前,是不是会自动调用所有setXXX的一些方法?为方便大家分析,我把几个文件的关键代码贴出来index.jsp: <form method="post" action="helloworld.action">
<p>输入你的名字:<br>
<input type="text" name="gname" /><br>
<input name="button1" type="submit" value="确定">
</p></form>
xwork.xml: <action name="helloworld" class="cn.HelloWorld">
<result name="success">/hello.jsp</result>
</action>HelloWorld.java:package cn;import com.opensymphony.xwork.Action;public class HelloWorld implements Action {
private String message;
private String guestname;
public String execute() throws Exception {
message = "hello world, "+ guestname +"! the time is: ";
message += System.currentTimeMillis();
return SUCCESS;
}
public String getmert() {
return message+" OK!";
}
public void setgname(String str) {
this.guestname = str;
}
}
hello.jsp:<%@ taglib prefix="ww" uri="/webwork" %><html>
<head>
<title>hello page</title>
</head> <body>
the message generated by my first action is :<br>
<ww:property value="%{mert}"/>
</body>
</html>
发送index.jsp中文本框的值到helloworld.action,接着读取xwork.xml文件,然后执行HelloWorld这个类中的execute()方法(在执行方法之前就通过setXXX的一些方法设置了类变量的值),然后转到hello.jsp页面,执行自定义property标签对应action中的getmert方法,显示结果到浏览器。不知道我理解的有没有错?如果不是这样,那么这个处理过程是怎么样的?还有,执行execute()方法之前,是不是会自动调用所有setXXX的一些方法?为方便大家分析,我把几个文件的关键代码贴出来index.jsp: <form method="post" action="helloworld.action">
<p>输入你的名字:<br>
<input type="text" name="gname" /><br>
<input name="button1" type="submit" value="确定">
</p></form>
xwork.xml: <action name="helloworld" class="cn.HelloWorld">
<result name="success">/hello.jsp</result>
</action>HelloWorld.java:package cn;import com.opensymphony.xwork.Action;public class HelloWorld implements Action {
private String message;
private String guestname;
public String execute() throws Exception {
message = "hello world, "+ guestname +"! the time is: ";
message += System.currentTimeMillis();
return SUCCESS;
}
public String getmert() {
return message+" OK!";
}
public void setgname(String str) {
this.guestname = str;
}
}
hello.jsp:<%@ taglib prefix="ww" uri="/webwork" %><html>
<head>
<title>hello page</title>
</head> <body>
the message generated by my first action is :<br>
<ww:property value="%{mert}"/>
</body>
</html>
首先,取得你在index.jsp中输入的gname值,这是通过你定义的Model,里面应该定义有getGname和setGname方法,当然,你这里把这些方法定义在action里了应该也可以,不过一般都是写个专门的Model,接着,调用配置文件
<action name="helloworld" class="cn.HelloWorld">
<result name="success">/hello.jsp</result>
</action>
这里指出跳转到cn.HelloWorld这个类,完后,访问这个类,执行execute()方法,将字符串存入到message,返回SUCCESS,又根据配置文件<result name="success">/hello.jsp</result>,跳转到hello.jsp界面,输出你的结果,就是这样
估计webwork也是使用的BeanUtil包吧.执行action的execute()方法之前,框架已经从httpRequest中获取了各个属性值,通过反射机制调用这些setter方法,赋值给action的各个属性.