struts2環境搭建教程

一、環境準備

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

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

二、搭建web項目

1、項目文件結構:
在這裏插入圖片描述
2、配置
(1)導入必需架包:

序號 struts2 - 必需架包
1 commons-fileupload-1.4.jar
2 commons-lang3-3.8.1.jar
3 freemarker-2.3.28.jar
4 javassist-3.20.0-GA.jar
5 log4j-api-2.11.1.jar
6 ognl-3.1.21.jar
7 struts2-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關鍵字提示出的類進行選擇。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章