一、Eclipse helios 配置struts2圖解

原文鏈接:http://blog.csdn.net/xiazdong/article/details/7214967



本文章環境:

1. Eclipse for JavaEE developer   Helios

2. Struts 2.3.1.1

3. tomcat 7.0.6

配置前提:配置好tomcat,本文省略配置tomcat步驟


其實MyEclipse和Eclipse for JavaEE 的配置過程差不多,唯一的區別在於:

Eclipse for JAVAEE創建 Dynamic Web Projec;

MyEclipse創建的是Web Project;


 

1、創建一個Dynamic Web Project


 

2.點擊next



 

3.看到output folder爲build\classes,和傳統的WEB-INF\classes有所差別,但是開發時不需要注意;


 

4.

在WEB-INF中配置web.xml,爲了將Struts2框架添加入WEB應用;

在src中配置struts.xml(Eclipse在編譯時會將src目錄下的除了Java文件外的其他文件全部拷貝進WEB-INF\classes)

將struts核心類庫導入WEB-INF\lib中;



 

5.編寫web.xml


此處配置的目的是爲了將struts2框架融入web應用,此處配置了一個過濾器,從前面學習可以知道,過濾器的作用是在Servlet執行之前完成一些事情,從<url-pattern>中可以看出任意的請求都會進入struts2的框架的範疇;

[html] view plaincopy
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  
  3.   <filter>  
  4.     <filter-name>struts2</filter-name>  
  5.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  6.   </filter>  
  7.   <filter-mapping>  
  8.     <filter-name>struts2</filter-name>  
  9.     <url-pattern>/*</url-pattern>  
  10.   </filter-mapping>  
  11. </web-app>  


6.編寫struts.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="GBK" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.       
  8. </struts>  


 

編寫Hello world

1.創建一個Hello.jsp 並且內容爲Hello struts2!!!

2.配置struts.xml

[html] view plaincopy
  1. <struts>  
  2.     <constant name="struts.devMode" value="true"></constant>  
  3.     <package name="HelloPackage" namespace="/" extends="struts-default">  
  4.         <action name="Hello">  
  5.             <result>/Hello.jsp</result>  
  6.         </action>  
  7.     </package>  
  8. </struts>  


3.部署並在瀏覽器中填寫  http://localhost:8888/StrutsDemo01/Hello

 

注意:在以後的開發中必須在<struts>元素中添加:

<constant name="struts.devMode" value="true"></constant>

因爲這表明是在開發者模式,是指發生錯誤時提供更多的提示信息;

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