快速搭建struts2框架
1.下载struts2源码包,官方网址:https://struts.apache.org/download.cgi#struts2516
2.创建一个web工程项目
3.打开struts2源码包的lib文件夹,选择需要的jar包:
commons-io
commons-lang3
commons-fileupload
javassist
struts2-core
xwork-core
freemarker
ognl
4.将所需jar包拷贝到lib目录下:
5.在src下创建名字为struts.xml的配置文件,在struts2-core的jar包下有一份xml的dtd校验文件:
6.在web.xml中配置struts2启动过滤器:
<!-- 配置struts2启动过滤器 -->
<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>*.do</url-pattern>
</filter-mapping>
7.在WEB-INF下创建一个测试的jsp文件hello.jsp(WEB-INF下的页面是不能直接访问的):
<%@ 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>Insert title here</title>
</head>
<body>
<b>你好!欢迎使用struts2框架</b>
</body>
</html>
8.创建一个HelloAction类
package com.xxj.action;
/**
* 测试struts2框架
* @author aloys
*
* @date 2018年7月28日 下午3:22:04
*/
public class HelloAction {
public String sayHello(){
return "success";
}
}
9.在struts.xml配置文件中进行请求后缀的更改(默认请求后缀是action),配置action
<!-- 更改请求默认后缀 -->
<constant name="struts.action.extension" value="do"></constant>
<package name="default" extends="struts-default" namespace="/hello">
<action name="hello" class="com.xxj.action.HelloAction" method="sayHello">
<result name="success">/WEB-INF/hello.jsp</result>
</action>
</package>
10.发布项目到服务器,启动,访问地址:http://localhost:8081/StrutsDemo/hello/hello.do运行成功