1.建立新project Structs2
2.在web.xml中加入一下代码
<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>
3.建立my.struts2.web.HelloWorldAction类
package my.struts2.web;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
public String execute() {
name = "Hello, " + name + "!";
return SUCCESS;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.在my.struts2.web包下建立struts_hello.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>
<include file="struts-default.xml"/>
<package name="tutorial" extends="struts-default">
<action name="hwAction" class="my.struts2.web.HelloWorldAction">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>
5.在类根目录下建立struts.xml(和log4j.properties配置文件同一个目录)
<?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>
<constant name="struts.devMode" value="true" />
<include file="my/struts2/web/struts_hello.xml"/>
</struts>
6.建立sayHello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Say Hello</title>
</head>
<body>
<h3>Say "Hello" to: </h3>
<s:form action="hwAction">
Name: <s:textfield name="name" />
<s:submit/>
</s:form>
</body>
</html>
7.建立HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="name" /> </h3>
<h3>Congratulations!!!!! </h3>
</body>
</html>
8.用到的jar包有
如果项目中不使用freemarker可以在struts2-core-2.1.8.1.jar包中找到structs-default.xml中的
<bean class="org.apache.struts2.views.freemarker.FreemarkerManager" name="struts" />
这段配置后注解掉就Ok,其他无用的包可以依次类推。
以上是strcuts2.0的基本配置。