struts2环境搭建教程

    xiaoxiao2022-07-08  210

    一、环境准备

    1、struts-2.5.20 2、Tomcat 6.0 3、jdk 1.7.0

    struts2官方架包下载可参考我的另一篇文章:三大框架官方架包下载方法

    二、搭建web项目

    1、项目文件结构: 2、配置 (1)导入必需架包:

    序号struts2 - 必需架包1commons-fileupload-1.4.jar2commons-lang3-3.8.1.jar3freemarker-2.3.28.jar4javassist-3.20.0-GA.jar5log4j-api-2.11.1.jar6ognl-3.1.21.jar7struts2-core-2.5.20.jar

    (2)web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>struts2test</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>action2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>action2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

    (3)index.jsp

    <body> <form action="test" method="post"> <input type="submit" value="测试struts2"> </form> </body>

    (4)default.jsp

    <body> struts2请求成功 </body>

    (5)TestAction.java

    package com.test.action; import com.opensymphony.xwork2.Action; public class TestAction implements Action{ @Override public String execute() throws Exception { System.out.println("action测试成功!"); return SUCCESS; } }

    (6)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"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="test" class="com.test.action.TestAction"> <result name="success">/default.jsp</result> </action> </package> </struts>

    运行成功,环境搭建成功

    相关说明:

    1、struts2版本与jdk版本与tomcat版本要对应: struts-2.5.20是当前最新版本,但对应jdk版本为jdk 1.7.0(参考:https://blog.csdn.net/u013020593/article/details/90483914); tomcat版本选择tomcat6.0较为合适,某大侠试过tomcat7.0,结果花了几个小时的时间都没解决掉报错。 2、配置web.xml时,需要使用到struts2中的StrutsPrepareAndExecuteFilter.java,如果不知道该类全拼,可以通过新建测试类并利用struts关键字提示出的类进行选择。

    最新回复(0)