开发环境:win 8
服务器:tomcat 7
开发工具:myeclipse 10
Struts2版本号:struts-2.5.10.1
JDK版本:JDK 1.6
1.工程建立以及web.xml配置
首先创建一个名为StrutsDemo的web工程,然后配置web.xml文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>StrutsDemo</display-name>
<filter>
<!--Filter名称-->
<filter-name>struts2</filter-name>
<!--Filter入口-->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!--Filter名称-->
<filter-name>struts2</filter-name>
<!--拦截所有的url-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意:
在Struts2.5中filter-class的路径应该是:org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
Struts2.5之前的版本的路径为:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
2. 倒入工程所需要的jar包:
注意:
1. myeclipse下直接导入lib下即可,eclipse中导入lib之后还需要在工程中构建路径,即右键工程名->build path->configure build path->添加外部jar->选择你导入jar包;
2. 在struts2.5版本中xwork-core-xxxx.jar这个包已经和struts2-core-xxxx.jar合在一起了
3. 编写struts.xml控制器文件,如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!--制定struts.xml文件的根元素-->
<struts>
<!--制定web编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!--配置包,包名为default,该包继承了Struts2框架的默认包struts-default-->
<package name="default" namespace="/" extends="struts-default">
<!--定义名为hello的Action,该Action的处理类为com.action.TestAction,并映射到success.jsp-->
<global-allowed-methods>add, update</global-allowed-methods>
<action name="hello" class="com.action.TestAction">
<!--name默认值为"success"-->
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
注意:
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 这一句在Struts2.5的版本中必须加,是因为我们创建的是动态的web工程,在Struts2.5之前的版本会自动调用DynamicMethodInvocation类,但是在Struts2.5之后必须自己手动设置才可以。
4. 前段的index.jsp和success.jsp页面
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
This is my JSP page. <br>
<s:a action="hello">hello</s:a>
</body>
</html>
注意:使用s标签时,必须导入struts2-core-2.5xx.jar包或者xwork-core-xxxx.jar+struts2-core2.5之前的版本
success.jsp:(只带body部分,其余部分与index.jsp相同故省略)
<body>
This is my JSP page. <br>
<s:property value="helo"/>
</body>
5. 后台Struts程序TestAction
TestAction.java:
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String helo;
public String getHelo() {
return helo;
}
public void setHelo(String helo) {
this.helo = helo;
}
public String execute() throws Exception {
helo="hello,world";
return SUCCESS;
}
}
6. 允许结果图
注意:如果在程序运行中出现了如下错误:
http status 500 - /index.jsp (line: 2, column: 42) File "/struts-tags" not found
与上面这个错误相同的还会偶尔出现另一个500的错误可能如下:
http status 500-java.lang.xxxxNot found -index.jsp??(大致如此,具体的没有复制忘记了)
很有可能的是tomcat服务器的问题,我建议你重新安装服务器
如果出现如下问题:
java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter
则在服务器配置文件context.xml中加上<Loader delegate="true"/>