个人随笔
目录
环境搭建:Strtus2.5.26环境搭建(简单例子)
2020-12-16 14:34:01

哎!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的

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>Struts2526</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <filter>
  8. <filter-name>struts-prepare</filter-name>
  9. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>struts-prepare</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>
  15. </web-app>

四、创建Action类

  • 1.extends(继承) ActionSupport
  • 2.返回的字符串用于结合配置文件进行跳转
  1. package com.suibibk.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class IndexAction extends ActionSupport {
  4. private static final long serialVersionUID = 8193673411420661865L;
  5. /**
  6. * 1.每一个对外的方法,都是返回String类型
  7. * 2.返回的字符串,要跟配置文件一一对应,用于跳转
  8. * @return
  9. */
  10. public String index2(){
  11. System.out.println("我是action,被struts调用");
  12. return "success";
  13. }
  14. }

五、新建一个struts.xml配置文件。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5. <!-- 上面的头,注意版本,从样例里复制过来
  6. showcase.war\WEB-INF\src\java\struts.xml
  7. -->
  8. <struts>
  9. <!-- 第1步:先定义一个包 -->
  10. <package name="mypck" extends="struts-default">
  11. <!--
  12. 第2步:定义一个action,配置跳转信息
  13. name 类似于Servlet @WebServlet("/IndexServlet")
  14. class 对应于自己写的Action类 当不写method属性时,默认调用的是execute
  15. http://ip:port/Struts2526/indexAction
  16. -->
  17. <action name="index3" class="com.suibibk.action.IndexAction" method="index2">
  18. <!-- 配置不同字符串,跳转到不同的页面. 当Action中的execute方法,返回字符串success,就跳转到index.jsp -->
  19. <result name="success">index.jsp</result>
  20. </action>
  21. </package>
  22. </struts>

六、创建index.jsp页面

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. 第一个struts2框架搭建成功!
  22. </body>
  23. </html>

七、发布测试

这里必须用jdk1.8以上编译,用1.7编译会报错,并且我这里试了貌似用tomcat7也不行,必须用tomcat8,启动后访问如下链接:

访问链接:http://localhost:8080/Struts2526/index3 可以看到访问成功。
访问链接:http://localhost:8080/Struts2526 也会跳到web.xml里面配置额welcome页面index.jsp

收工!

 458

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2