探索与发现

研究java技术

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  83 随笔 :: 0 文章 :: 109 评论 :: 0 Trackbacks

本地中文化网址

http://wiki.javascud.org/display/ww2cndoc/Home

在线文档

http://www.opensymphony.com/webwork/wikidocs

struts 的区别有实现国级化的时候, struts message 只能出现 5 个动态的值

也就像下面的信息

Your {0} ticket(s) for flight {1} have been booked.

The total cost is {2}. Your confirmation number is {3}.

Your flight leaves at {4} and arrives at {5}

需要分成两段才能完成

<bean:message key="confirmation.msg1"

arg0="count" arg1="flightNumber" arg2="cost"/>

<bean:message key="confirmation.msg2"

arg0="confirmation" arg1="departure" arg2="arrival"/>

webwork 是无限制的,像上面的可以写成这样子

<ww:text name="confirmation.msg">

<ww:param value="count"/>

<ww:param value="flightNumber"/>

<ww:param value="cost"/>

<ww:param value="confirmation"/>

<ww:param value="departure"/>

<ww:param value="arrival"/>

</ww:text>

还有在web.xml里同指定webwork的tld所在位置

<taglib>

<taglib-uri>webwork</taglib-uri>

<taglib-location>
/WEB-INF/lib/webwork-2.1.7.jar

</taglib-location>

</taglib>
在很多书上都讲上面可行,可是我却报下面的错...换成下面那种方式就ok了

org.apache.jasper.JasperException: Unable to initialize TldLocationsCache: null
也可以指定webwork.tld所在的位置也可以,,如
/WEB-INF/webwork.tld(webwork.tld拷到这下面来).

一个简单的例子
 
import com.opensymphony.xwork.Action;
public class HelloWorldAction implements Action{
    String greeting;
    public String getGreeting() {
        return greeting;
    }
    public String execute() throws Exception {
        greeting = "Hello World!";
        return SUCCESS;
    }
}
实现的action接口里面只有一个方法
 
interface Action {
String execute() throws Exception;
}
greetings.jsp
<%@ taglib prefix="ww" uri="webwork" %>
<html>
<head>
    <title>First WebWork Example</title>
</head>
<body>
<p><ww:property value="greeting"/></p>
</body>
</html>
xwork.xml 放到就用的classpath下面(也就web-inf/classes下面)
<action name="hello" class="HelloWorldAction">
<result name="success" type="dispatcher">
<param name="location">/greetings.jsp</param>
</result>
<interceptor-ref name="debugStack"/>
<interceptor-ref name="defaultStack"/>
</action>
还有在web.xml里面加上webwork的一个加载类
<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
ActionSupport
1:提供对错误信息的支持
action and field specific errors
field errors are automatically supported by views
2:国际化的支持
1 resource bundle per action
pervasive UI support for retrieving messages
 --------------------------------------------------------------------------------
ActionSupport Example:
import com.opensymphony.xwork.ActionSupport;
public class HelloWorldAction extends ActionSupport{
    String greeting;
    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }
    public String getGreeting() {
        return greeting;
    }
    public String execute() throws Exception {
        if (greeting.equals("") || greeting == null) {
            addFieldError("greeting", getText("greeting"));
            return ERROR;
        }
        return SUCCESS;
    }
}
这里的getText("greeting")的greeting要与HelloWorldAction.properties(放在与HelloWorldAction.class同级目录下面,名称也一样)里的greeting=Enter your greeting please相同
上面的set.get其实与struts当中的ActionFrom类似..
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="ui" uri="webwork" %>
<html>
<head>
    <title>First WebWork Example</title>
</head>
<body>
<form action="hello.action" method="post">
<table>
    <ui:textfield label="欢迎语句" name="greeting"/>
    <ui:submit value="'Submit'"/>
</table>
</form>
</body>
</html>
这里的greeting要与HelloWorldActio.java里的属性一致
如出现错误会显示在当前页,
Enter your greeting please

如填上内容,就会相应的调用setGreeting()方法的..
<%@ taglib prefix="ww" uri="webwork" %>
<html>
<head>
    <title>First WebWork Example</title>
</head>
<body>
<p><ww:property value="greeting"/></p>
</body>
</html>
把填的内容显示出来
在xwork.xml里的<action>
也相应的变成
<action name="hello" class="helloWorld.HelloWorldAction">
   <result name="success" type="dispatcher">
    <param name="location">/greetings.jsp</param>
   </result>
   <result name="error" type="dispatcher">
          <param name="location">/index.jsp</param>
            </result>
  </action>
------------------------------------------------------------
 
Model-Driven Actions vs. Field-Driven Actions
2 types of Actions possible:
(1)Field-Drivern上面的例子就是
(2)ModelDriven
package helloWorld;
public class Pet {
    private long id;
 private String name;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
--
package helloWorld;
import com.opensymphony.xwork.*;
public class AddPetAction extends ActionSupport implements ModelDriven{
 Pet pet = new Pet();
    public Object getModel() {
        return pet;
    }
    protected void doValidation() {
        if (pet.getId() == 0) {
            addFieldError("id", "Please enter in an id number for you new Pet.");
        }
    }
    public String doExecute() throws Exception {
        if (hasErrors())
            return ERROR;
        return SUCCESS;
    }

}
需实现ModelDriven这个接口,它也只有一个方法
public Object getModel();
 在xwork.xml加上
<action name="addpet" class="helloWorld.AddPetAction">
   <result name="success" type="dispatcher">
    <param name="location">/petadded.jsp</param>
   </result>
   <result name="error" type="dispatcher">
    <param name="location">/addpet.jsp</param>
   </result>
   <interceptor-ref name="debugStack" />
   <interceptor-ref name="defaultStack" />
  </action>
还有两个页面分别用来添加和显示用的
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="ui" uri="webwork" %>
<html>
<head>
    <title>First WebWork Example</title>
</head>
<body>
<form action="addpet.action" method="post">
<table>
    <ui:textfield label="Pet ID Number" name="id"/>
    <ui:textfield label="Name of Pet" name="name"/>
    <ui:submit value="'Submit'"/>
</table>
</form>
</body>
</html>
-------petadded.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="ww" uri="webwork" %>
<html>
<head>
    <title>First WebWork Example</title>
</head>
<body>
<p>Added a pet with the following properties:</p>
<ul>
    <li>ID: <ww:property value="id"/></li>
    <li>Name: <ww:property value="name"/></li>
</ul>
</form>
</body>
</html>
posted on 2006-08-02 19:02 蜘蛛 阅读(3270) 评论(0)  编辑  收藏 所属分类: webwork

只有注册用户登录后才能发表评论。


网站导航: