准备:1.安装好Tomcat的eclipse
2.Structs2.3.24 http://struts.apache.org/download.cgi#struts2324
步骤一:解压Structs2
将下载下来的Structs2.3.24压缩包解压,得到如下图所示文件:
将apps目录下的structs2-blank.war文件单独解压到一个新建文件夹中,得到如下图所示目录:
步骤二:建立Java Web项目
新建一个名为Hello的Java Web项目,初始项目目录如下图所示:
并将由war包解压出来的文件中WEB-INF/lib目录下的所有jar包全部复制到Hello工程的WEB-INF下的lib文件夹中,一个都不能少。
步骤三:配置stucts.xml文件
将war包解压出来的WEB-INF/src/java目录下的structs.xml文件复制到Hello工程的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="default" namespace="/" extends="struts-default">
<action name="hello">
<result >
/hello.jsp
</result>
</action>
</package>
</struts>
action中的name对应的是最后请求的时候传递的类似于参数的东西,之后会要对应上。result标签中包裹的是对应请求返回的内容,例子中就是返回目录下hello.jsp这个文件。
步骤四:配置web.xml文件
将下面内容复制到web.xml中
<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>
这是配置Structs2中的过滤器。
步骤五:编写jsp文件
在Hello工程下新建一个jsp文件,其内容为:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
HelloWorld!
</body>
</html>
需要在后面在加上hello访问
这样简单的Structs2的配置的完成了。其实应该将structs.xml文件放在WEB-INF的classes文件夹下。
还有关于structs.xml中的配置还有很多,这个只是一个简单的映射,还有要学的。