Java EE之Struts2-2.5配置
開學以來,已經三周了。Java EE卻不太走心,於是,這幾日空杯心態,重新學習。復習了Java SE和Java Web開發技術,然後入手Struts2。為了使用最新版本的Structs2,我去官網下載了2.5版本的Struts2.,然後,然後,然後,就被坑了。
一、下載Struts2
官網:http://struts.apache.org/
[博主使用的是2.5]
二、創建Java Web Project
[略]
三、配置web.xml【關鍵點】
在/WebRoot/WEB-INF/下新建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_3_0.xsd" version="3.0"> <display-name>Struts2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- struts2-2.3及其以下配置↓ --> <!-- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> -->
<!-- 原因是2.5版本中已經不存在ng包了 --></filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <!-- <url-pattern>*.action</url-pattern> --> <!-- url:http://314649444.iteye.com/blog/1860338 --> <!-- Error:HTTP Status 500; The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location] --> <!-- 如果使用strus2標簽,則需要在使用之前當前請求要經過Struts2的核心處理,即Struts2的核心控制類,而我在web.xml中配置Struts2過濾器只配置了<url-pattern>*.action</url-pattern>僅處理了後綴名為action的請求,而直接訪問jsp是不會處理的, 顧將此處改成<url-pattern>/*</url-pattern>後問題解決 --> </filter-mapping> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>xhu.edu.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login2.jsp</welcome-file><!-- STRUTS2 --> <welcome-file>login.jsp</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
四、配置struts.xml
在src下新建struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<!-- 用戶登錄(user login) -->
<action name="login" class="xhu.edu.action.LoginAction">
<result name="success">/main2.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
五、OK
Congratulations!
六、文獻:http://314649444.iteye.com/blog/1860338
Java EE之Struts2-2.5配置