一。
主要配置文件 :
下面是您需要注意的所有文件.为了使开发更容易,其中的一些是可以动态重新加载的
WebWork有两个主要配置文件:web.xml 和 xwork.xml
一 :web.xml
如果要用到WebWork的最新特性并且不需要考虑向后兼容问题,您只需要在web.xml中 :
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
如果要用JSP的话,还需要
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
但是如果是从2.1.7或更早的版本升级的Web应用程序,那么需要做更多的工作使程序能够正常运行.更多信息请查看web.xml 2.1.x兼容性.
过滤器配置如下:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/ca75c07623e1b494fee67e8f316fc310.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
如果要用到JSP,标签库配置如下:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
译者注:如果是servlet2.4,标签库配置如下:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
如果你要使用 SiteMesh 进行页面装饰,你应该需要添加一些 额外的过滤器
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/ca75c07623e1b494fee67e8f316fc310.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
脚注:
WebWork允许动态装载xml配置文件(例如,重新装入actions.xml).
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
为了开启这个特性,在你的webwork.properties文件里添加下面的内容:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
二 :xwork.xml的例子
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
33
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
34
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
很长时间没写笔记了,研究了一段时间的webwork,今天写点学习笔记之类的东西。
Webwork是一个MVC框架,和其他MVC框架一样,分离web开发中的,控制,模型和视图。个人感觉,她的配置更加简单和优雅,更容易新人上手。同时,业务逻辑也更加清晰,对软件的维护和扩展有很大的帮助。(有多少帮助也不是很清楚,还是讲讲配置把!)
需要的包,去baidu或者google都能找到很多。现在常用的版本是 2.2.4 。2.1版本和2.2版本还是有不少区别,如果想学习,还是多看看介绍2.2的教程。
建立工程,加载所需要的包就不说了。
看下重要的配置文件把:
Web.xml 配置核心控制器:
<filter>
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
可说的也没多少,照这例子配置的,那个*.action是你要处理的文件的后缀名,例如提交的表单中的action=”*.action” 这样就可以调用指定的处理类了。
WEB-INF/classes 目录下:xwork.xml. 配置action的地方。
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="testAction" class="testAction" method="test">
<result name="success">/view.jsp</result>
</action>
</package>
</xwork>
具体的action配置规范可以参考 相关文档。
重点说两个有用的东东,第一个是拦截器,第二个是视图技术。
拦截器能过滤很多东西,例如:你表单中的参数是否合法等。简化你的编码工作。
视图就不说了,jsp,pdf,自定义标签等。Webwork都支持,其中官方还推进一种模版技术,freemarker。(这个还在研究中,很不错的东西。)
下面来看看action怎么写。
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionSupport;
public class LoginAction extends ActionSupport implements Action {
// 必需进行初始化
private User user = new User();
public User getUser() {
return user;
}
public String execute() throws Exception {
// 可以在这里调用Service层来进行验证,这里只验证用户名
if (user.checkResult(user.getUserName(),user.getUserPwd()))
return this.SUCCESS;
else
return this.ERROR;
}
}
这个是model类的例子:
public class User {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public static void main(String[] args) {
// TODO 自动生成方法存根
}
public boolean checkResult(String userName,String userPwd){
String username=userName;
String userpwd=userPwd;
if(username.equals("totti")||userpwd.equals("totti")){
return true;
}
return false;
}
}
需要提交的表单:
<form id="loginForm" name="myForm" method="post" action="login.action" >
用户名:
<label>
<input type="text" name="user.userName" />
</label>
<p>密码:
<label>
<input type="text" name="user.userPwd" />
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</p>
</form>
配置的action
<action name="login" class="webWork.LoginAction">
<!-- 这里的意思是登录成功后跳转到 /ok.jsp 页面 -->
<result name="success" type="dispatcher">
<param name="location">/view/ok.jsp</param>
</result>
<!-- 登录失败后则返回登录页 -->
<result name="error" type="dispatcher">
<param name="location">/view/err.jsp</param>
</result>
<result name="input" type="dispatcher">
<param name="location">/view/login.jsp</param>
</result>
<!-- 此段代码为作用是将request请求的参数传递到 action 中 -->
<interceptor-ref name="params" />
<interceptor-ref name="validationWorkflowStack"/>
</action>
可以看到,提交过来的数据会被设置器方法设置,在model中实现数据的处理,然后返回给控制器。
上面给出的是代码的片段,应该不能直接正常运行。如果想得到范例代码,就留下联系方式,我会给你发一份代码的。
下次,会介绍下webWork +spring 的配置方式,利用spring动态注入action是对action管理的最佳解决方案。
-
三。
Webwork2.2的配置文件变化
-
在webwork中, com/opensymphony/webwork/default.properties是很重要的文件,它对webwork的参数进行了配置,其中包 含了webwork支持的所有配置,如果你想覆盖其中的配置,只需要建立一个webwork.properties放在你的项目中(一般放在源码目录下, 发布时的位置是WEB-INF/classes下).
让我们来看看在webwork 2.2中这个文件发生了什么变化.
1.可以配置webwork.objectFactory# if specified, the default object factory can be overridden here
#webwork.objectFactory = com.opensymphony.xwork.spring.SpringObjectFactory
这个ObjectFactory是一个功能很大的类,默认webwork使用的是 com.opensymphony.xwork.ObjectFactory,如果你想自己实现一个或者使用Spring的机制,可以配置这个属性.不过 在没有更详细的文档出来之前,一定要小心使用.SpringObjectFactory类中主要覆写了ObjectFactory的buildBean,并没有替换其他函数.
2.webwork.mapper.class
# extension for actions
webwork.mapper.class=com.opensymphony.webwork.dispatcher.mapper.DefaultActionMapper
webwork.action.extension=action
可以看到,增加了一行webwork.mapper.class.
DefaultActionMapper以及同目录下RestfulActionMapper相当于以前的普通action url路径处理和CoolUriServletDispatcher映射方式处理,在webwork2.2中,如果想使用 CoolUriServletDispatcher的方式,在此处修改这个属性为RestfulActionMapper即可.当然,你可以实现自己的ActionMapper,实现自己的想法,很多人都喜欢自己来处理url,那就自己写一个,不错的想法.
3.webwork.tag.altSyntax=true
altSyntax语法默认为打开了,参考前面的文章4.webwork.i18n.reload
# when set to true, resource bundles will be reloaded on _every_ request.
# this is good during development, but should never be used in production
webwork.i18n.reload=false如果设置为true,每次资源文件都会被重载,方便调试.发布后一定要记得改为false.
5.webwork.ui.templateSuffix
#sets the default template type. Either ftl, vm, or jsp
webwork.ui.templateSuffix=ftl
Tag模板的默认配置为freemarker的ftl了,以前是vm的.
6.webwork.dispatcher.parametersWorkaround# workaround for some app servers that don’t handle HttpServletRequest.getParameterMap()
# often used for WebLogic, Orion, and OC4J
webwork.dispatcher.parametersWorkaround = false
这个是为了处理一些web server的非常规的运作行为的,具体我就没试过了.