Webwork 学习笔记

1. 首先配置一个简单的webwork应用

核心jar:

commons-logging.jar
ognl.jar
oscore.jar
velocity-dep.jar
webwork-2.1.7.jar
xwork.jar

  1. 配置web.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    <servlet>
    <servlet-name>webwork</servlet-name>
    <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>webwork</servlet-name>
    <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <taglib>
    <taglib-uri>webwork</taglib-uri>
    <taglib-location>/WEB-INF/lib/webwork-2.1.7.jar</taglib-location>
    </taglib>
    </web-app>
  2. 新建action: HelloWorld.java  
     package com.aeolia.action;
    
     import java.text.SimpleDateFormat;
    import java.util.Date; import com.opensymphony.util.Data;
    import com.opensymphony.xwork.Action; public class HelloWorld implements Action {
    private String message;
    private String name; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } @Override
    public String execute() throws Exception {
    String result = "";
    if (this.name == null || this.name.equalsIgnoreCase("world") || this.name.equals("")) {
    result = INPUT;
    message = "Blank name or names of world are not allowed ";
    } else {
    message = "Hello World ! ";
    java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    String dateString = format.format(date);
    message += this.name;
    message += " time is " + dateString;
    result = SUCCESS;
    }
    return result;
    } public String getMessage() {
    return message;
    } }
  3. 配置xwork.xml(位于classes下)
    <!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">
    <default-interceptor-ref name="completeStack" />
    <action name="HelloWorld" class="com.aeolia.action.HelloWorld">
    <result name="success">hello.jsp</result>
    <result name="input">name.jsp</result>
    </action>
    </package>
    </xwork>
  4. 新建view:hello.jsp 
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="MS932"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <%@taglib prefix="ww" uri="webwork"%>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    Show message
    <ww:property value="message" />
    </body>
    </html>

完成,现在可以访问http://localhost:8080/web/HelloWorld.action

下图为简易的时序图

Webwork 学习笔记

上一篇:[C++ Primer Plus] 第8章、函数探幽(一)程序清单——内联、引用、格式化输入输出、模板、decltype


下一篇:当通过Nuget包管理器获取还原组时,出现 提示 “xxxxx”已拥有为“xxxxx”定义的依赖项