官方网站上面用Maven挺方便的, 问题是太方便了, 有些细节都不太清楚, 还是用Eclipse自己配一个玩玩。
1. 首先新建一个叫struts2的Dynamic Web Project工程
2.在Java Resources:src下面新建一个tutorial的包,并且在此包中建一个HelloWorld.java类(此类的内容见最后附文)。
3.在Java Resources:src下面新建两个XML文件,struts.xml和helloworld.xml(见附文)。[注,最容易犯错的就是这里, 往往习惯性的放到WEB-INF下面去了]
4.下载 struts的核心依赖包,解压缩后,将5个文件拷贝到lib下面(见图中的lib目录)
5. 修改web.xml文件,添加FilterDispatcher类(见附文)。
6. 新建index.jsp和helloworld.jsp文件(见附文)。
附文:
struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Chapter 01 Hello World -->
<include file="helloworld.xml" />
</struts>
helloworld.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloworld" extends="struts-default">
<action name="helloworld" class="tutorial.HelloWorld">
<result>/helloworld.jsp</result>
</action>
</package>
</struts>
HelloWorld.java文件
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
setMessage("Hello World!");
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2</display-name>
<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>
<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>
</web-app>
helloworld.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>Hello World Result</title>
</head>
<body>
<center>
<s:property value="message" />
</center>
</body>
</html>
index.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Struts2 Tutorial</title>
</head>
<body>
<center>
<table bgcolor="yellow" border="1">
<tr align="center">
<td colspan="2"><b>Content</b></td>
</tr>
<tr align="center">
<td width="400"><b>Chapter 01</b></td>
<td width="400"><a href="helloworld.action">Hello World</a></td>
</tr>
</table>
</center>
</body>
</html>