叶明的javablog

java学习天堂,个人关于其他blog:blog.javaworker.cn,欢迎大家访问
posts - 64, comments - 50, trackbacks - 0, articles - 3

经历一些繁忙的工作,开始学习webwork.

Posted on 2007-07-27 13:50 天空苍茫 阅读(392) 评论(0)  编辑  收藏 所属分类: jsp学习eclipse

最近由于struts2比较流行起来,我开始想学webwork2买了本书《webwork in action》中文版,但是却发现,讲解按照是2.1.7新的,现在都是2.2.6.了,变化最大(一个简单的helloworld例子:)

web.xml
<?xml version="1.0" encoding="GB18030"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <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>/*</url-pattern>
 </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">

<xwork>
 <include file="webwork-default.xml"></include>
 <package name="default" extends="webwork-default">
  <action name="helloWorld" class="cn.javaworker.yeming.action.HelloWorld">
   <result name="success">index.jsp</result>
   <result name="input">hello.jsp</result>
  </action>
 </package>
</xwork>

hello.jsp
<%@ taglib prefix="ww" uri="/webwork"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>Enter you name</title>
  </head>
 
  <body>
  <ww:form action="helloWorld">
   <ww:textfield label="please enter user name" name="name"></ww:textfield>
   <input  type="submit"/>
  </ww:form>
   <form action="helloWorld.action">
     please enter you name:<input type="text" name="name" value="<ww:property value="name"/>"/>
    <input type="submit">
   </form>
  </body>
</html>

index.jsp

<%@ taglib prefix="ww" uri="/webwork"%>
<html>
 <head>
  <title>hello page</title>
 </head>
 <body>
  The message generated by my first action is :<ww:property value="message"/>
 </body>
</html>

HelloWorld.java

package cn.javaworker.yeming.action;

import com.opensymphony.xwork.ActionSupport;

/**
* @author $author
*/
@SuppressWarnings("serial")
public class HelloWorld extends ActionSupport{
 
 private String message;
 
 private String name;
 /**
  * @see com.opensymphony.xwork.Action#execute()
  */
 public String execute() throws Exception
 {
  if(name==null||name.equals("")||name.equals("world")){
   addFieldError("name","blank names or names of 'world' are not allowed!");
   return INPUT;
  }
  message ="hello "+name+"!\n";
  message+="this is the time is: "+System.currentTimeMillis();
  return SUCCESS;
 }
 /**
  * @return the name
  */
 public String getName() {
  return name;
 }
 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return the message
  */
 public String getMessage() {
  return message;
 }


}


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


网站导航: