Java EE之Struts2-2.5配置

來源:互聯網
上載者:User

標籤:style   init   pattern   ping   登入   cut   type   過濾   ini   

開學以來,已經三周了。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配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.