001-maven构建struts项目
maven构建struts项目
本例使用工程名:003maven_struts2,tomcat中间件端口:8080
创建maven的web项目参照:maven项目与myEclipse
1、pom.xml配置struts2包
在pom.xml的dependencies元素添加:
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.8</version> </dependency>
此处选用struts2-core的2.3.8版本。如需使用其他版本,可以到:http://mvnrepository.com/ 找到添加内容
2、web.xml配置struts2
在web.xml的web-app元素添加:
<filter>
<filter-name>struts2filer</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2filer</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
3、添加struts.xml配置
src/main/config目录下新建struts.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- 默认放在src根目录下,名称为struts.xml -->
<struts>
<package name="config" namespace="/" extends="struts-default">
</package>
</struts>
4、添加struts.properties配置
src/main/resources目录下新建struts.properties文件,内容如下:
#指定默认编码集,对于请求参数带有中文的情况应该设置陈GBK或GB2312.默认值UTF-8 struts.i18n.encoding=GB2312 #但struts.xml改动后,是否重新加载该文件。在开发阶段建议将此属性设置为“true”,提高开发效率。默认值false struts.configuration.xml.reload=true #是否使用Struts2的开发模式,可以获得更多报错信息,便于调试。在开发阶段设置为true。默认值false struts.devMode = true #设置浏览器是否缓存静态页面。开发阶段设置为false,以获得服务器的最新响应。默认值true struts.serve.static.browserCache=false
5、验证配置
src/main/config目录下struts.xml文件package元素添加内容:
<action name="say_hello" method="say_hello" class="com.cft.test.Test_say_hello"> <result name="success">/say_hello.jsp</result> </action>
src/main/java目录下新建包com.cft.test,在com.cft.test包下新建类:Test_say_hello,内容:
package com.cft.test;
public class Test_say_hello {
public String say_hello(){
System.out.println("hello,cft");
return "success";
}
}
src/main/webapp目录下新建say_hello.jsp文件,内容:
<html> <body> <h2>Hello cft!</h2> </body> </html>
tomcat部署,访问:http://localhost:8080/003maven_struts2/say_hello.action,界面:

控制台:

出现以上内容,说明配置成功。
浙公网安备 33010602011771号