一.环境准备
struts-2.3.4.1-all :下载地址:http://struts.apache.org/download.cgi#struts2341
二.工程配置
1. Eclipse:File-New-Dynamic Web project,新建一个工程【strutsDemo】
2. 将struts-2.3.4.1-all 解压,里面的lib文件夹中的以下jar文件
antlr-2.7.2.jar
aopalliance-1.0.jar
asm-3.3.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xstream-1.4.2.jar
xwork-core-2.3.4.1.jar
copy进刚刚创建的工程的WebContent\WEB-INF\lib里面,
之后在eclipse中右键该工程选择build Path,
在弹出窗口中选择第3选项卡libraries,
点击右侧的【Add Library】在弹出窗口中选择Web App libraries,将其引入。
三.struts2配置
1.struts2与struts1不同,使用filter作为整个框架的入口。在web.xml中加入以下语句
<!-- struts2 starter Filters -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.之后配置struts2自己的配置文件struts.xml,类似于struts1的struts-config.xml文件,不过因为加载方式的不同,这个文件存在于classpath中,即工程的src文件夹下。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="strutsdemo" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="hello" class="struts2.example.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
3.在src中加入在配置中指定的类文件HelloWorldAction
package struts2.example.action;
import struts2.example.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -1407265450788291203L;
private MessageStore messageStore;
public String execute() throws Exception {
messageStore = new MessageStore() ;
return SUCCESS;
}
public MessageStore getMessageStore() {
return messageStore;
}
public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
}
以及MessageStore
package struts2.example.model;
public class MessageStore {
private String message;
public MessageStore() {
setMessage("Hello Struts User");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4.添加jsp文件
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>
Helloworld.jsp
pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="messageStore.message" /></h2>
</body>
</html>
四.运行
总结:这个例子主要还是参照struts2的官方文档做的。
http://struts.apache.org/2.3.4.1/docs/guides.html
今后还会参照这个文档做些更复杂的例子。
另外发现struts通过convention-plugin实现了对annotation的支持,具体的内容请参照
http://struts.apache.org/2.3.4.1/docs/convention-plugin.html