哎!Struts真是让人百转千回,时不时搞个漏洞出来,然后我们苦逼的开发人员就得升级升级升级!这不,这次需要升级到最新版本,直接由2.3必须跳到2.5.26,没办法还是得搞,然后按网上的教程试了下,什么替换包,修改配置文件,最后还是失败,怪我们的项目太老,怪我们的jdk也太老,这怎么搞,听说struts2.5.26还必须jdk.1.8和tomcat8,喵了个咪,难难难!
不过俗话说,要攻克啥,得先了解啥啥啥的,我先试着用struts2.5.26搭个项目例子先。
一、新建项目
我这里用的是MyEclipse,其实用别的也一样,项目结构如下图:

二、导入jar包
这里直接用去Struts官网下载的struts-2.5.26-all.zip下面的例子struts-2.5.26\apps\struts2-showcase\WEB-INF\lib下面的jar包,全部拷贝到项目的lib目录下。
下载地址:http://struts.apache.org/download.cgi#struts2526
拷贝进去后,做如下操作:
- 把spring开头的删除
- 把struts2-spring-plugin-2.5.26.jar删除
- 把tiles开头的删除
三、在web.xml配置Struts2的过滤器
注:2.5.26版本的过滤器相比2.3*的少了个ng的
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>Struts2526</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>struts-prepare</filter-name><filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts-prepare</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
四、创建Action类
- 1.extends(继承) ActionSupport
- 2.返回的字符串用于结合配置文件进行跳转
package com.suibibk.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport {private static final long serialVersionUID = 8193673411420661865L;/*** 1.每一个对外的方法,都是返回String类型* 2.返回的字符串,要跟配置文件一一对应,用于跳转* @return*/public String index2(){System.out.println("我是action,被struts调用");return "success";}}
五、新建一个struts.xml配置文件。
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN""http://struts.apache.org/dtds/struts-2.5.dtd"><!-- 上面的头,注意版本,从样例里复制过来showcase.war\WEB-INF\src\java\struts.xml--><struts><!-- 第1步:先定义一个包 --><package name="mypck" extends="struts-default"><!--第2步:定义一个action,配置跳转信息name 类似于Servlet @WebServlet("/IndexServlet")class 对应于自己写的Action类 当不写method属性时,默认调用的是executehttp://ip:port/Struts2526/indexAction--><action name="index3" class="com.suibibk.action.IndexAction" method="index2"><!-- 配置不同字符串,跳转到不同的页面. 当Action中的execute方法,返回字符串success,就跳转到index.jsp --><result name="success">index.jsp</result></action></package></struts>
六、创建index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>第一个struts2框架搭建成功!</body></html>
七、发布测试
这里必须用jdk1.8以上编译,用1.7编译会报错,并且我这里试了貌似用tomcat7也不行,必须用tomcat8,启动后访问如下链接:
访问链接:http://localhost:8080/Struts2526/index3 可以看到访问成功。
访问链接:http://localhost:8080/Struts2526 也会跳到web.xml里面配置额welcome页面index.jsp
收工!
