struts2.0简单的例子

今天在apache网站上无意中看到了struts项目2.0.1正式发布了,怀着欣喜的心情我下载了stuts2.0.1完整包。 Struts2.0.1是struts项目和WebWork2.2项目的合并版本,集成了两大流行MVC框架的优点,对struts框架来说是一个大的提升,同时也更大程度地简化了开发人员的开发过程。我简单地研究了一下这个新版本,现在给大家介绍一个入门级的小例子,希望能对学习这个新版本的朋友有点帮助。
这个例子完成了一次URL调用返回结果页面的过程。
首先,要从apache网站上下载struts2.0.1的完整包(可以从这里下载:http://struts.apache.org/downloads.html),解压后需要找到下列几个文件:
commons-logging-1.0.4.jar
freemarker-2.3.4.jar
ognl-2.6.7.jar
struts2-api-2.0.1.jar
struts2-core-2.0.1.jar
xwork-2.0-beta-1.jar
然后,我们就开始做第一个例子,我们就使用经典的“HelloWorld”的名字吧!
1. 制作目录结构
如下图所示:
stuts2是web应用的根目录。
2. 拷贝引用文件
将上面列举的jar文件拷贝到步骤1中制作的目录struts2/WEB-INF/lib中。
3. 制作jsp文件HelloWorld.jsp

<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > Hello World !</ title >
</ head >
< body >
< h2 >< s:property value = " message " /></ h2 >
</ body >
</ html >

将该文件拷贝到步骤1中制作的目录struts2/example中。
4.制作java文件HelloWorld.java

package example;

/** */ /**
* <code>Set welcome message.</code>
*/
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport ... {

public static final String MESSAGE = "Struts is up and running ...";

public String execute() throws Exception ...{
setMessage(MESSAGE);
return SUCCESS;
}


private String message;

public void setMessage(String message)...{
this.message = message;
}


public String getMessage() ...{
return message;
}

}

使用下面的命令编译这个java文件:
set CLASSPATH=yourdirectory/xwork-2.0-beta-1.jar
javac HelloWorld.java
将编译后的HelloWorld.class文件拷贝到步骤1中制作的目录struts2/WEB-INF/classes/example中。
5.制作web应用的描述文件web.xml

<? xml version="1.0" encoding="UTF-8" ?>
< web-app 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"
       version
="2.4" >

< display-name > Struts Blank </ display-name >

< filter >
< filter-name > struts2 </ filter-name >
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
</ filter >

< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >

< welcome-file-list >
< welcome-file > index.html </ welcome-file >
</ welcome-file-list >
</ web-app >

将该文件拷贝到步骤1中制作的目录struts2/WEB-INF中。
6.制作MANIFEST.MF文件(从其它地方随便找一个即可)
将该文件拷贝到步骤1中制作的目录struts2/META-INF中。
7.制作struts配置文件struts.xml和struts.properties
struts.xml文件:

<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
< package name ="abc" namespace ="/example" extends ="struts-default" >
< action name ="HelloWorld" class ="example.HelloWorld" >
< result > /example/HelloWorld.jsp </ result >
</ action >
<!-- Add your actions here -->
</ package >
</ struts >

struts.properties文件:
struts.devMode = true
struts.enable.DynamicMethodInvocation = false
将这两个文件拷贝到步骤1中制作的目录struts2/ WEB-INF/classes中。
好了整个例子的文件我们都搞定了。
最后,你可以将制作好的web应用struts2拷贝到tomcat下运行。
访问http://localhost:8080/struts2/example/HelloWorld.action如果能够看到页面上的“Struts is up and running ...”提示信息说明你的例子是正确的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值