struts2 学习一 HelloWorld
开发步骤:
1、在Eclipse中新建一个web工程,并将Struts2框架的核心类库增加到WEB应用中。将Struts2框架下lib路径下的:
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
log4j-1.2.17.jar
ognl-3.0.6.jar
struts2-core-2.3.16.3.jar
xwork-core-2.3.16.3.jar
类库复制到新建工程WEB应用的lib目录下 ,也就是“%workspace%\example\WebContent\WEB-INF\lib”路径下 ,如图:
2、编辑WEB应用中的web.xml配置文件,配置struts2的核心Filter(过滤器)。下面增加了struts的核心Filter配置的web.xml配置文件代码片段
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>example</display-name>
<!-- 定义struts2的核心Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 让struts2 的核心拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、定义一个struts2的Action,Action通常应该继承ActionSupport基类。在Eclipse 中新建一个java类,该类命名为“HelloWorldAcion”,该类保存在example应用的src路径下的
编辑“HelloWorldAction”类的代码片段:
package com.example;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
// 定义用户请求的 execute()方法
public String execute() throws Exception {
setMessage("Hello World! ");
return SUCCESS;
}
}
4、添加 Action类后,还需要添加对应的配置文件,在src文件夹下添加struts.xml文件。修改后的代码如下:
<pre name="code" class="html"><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 所以action必须放定义在package中 -->
<package name="example" extends="struts-default">
<!-- 定义action的名字 ,及所对应的class -->
<action name="HelloWorld" class="com.example.HelloWorldAction">
<!-- 定义逻辑视图和物理资源之间的映射 -->
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
5、添加HelloWorld.jsp页面,完成action成功后跳转画面,画面代码如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>
<s:property value="message"/>
</h1>
</body>
</html>
OK,第一Struts2 实例完成啦, 是不是很无聊啊!!!
jeff
2014/9/20