1.下載myeclipse
2.file–new–other–Web Project
3.配置tomcat路徑 ,使用此配置的路徑對應的tomcat來發布web程序
- Windows–preferences–myeclipse–Servers–Tomcat
- 將其改爲enable
4.配置運行時的jdk
- Windows–preferences–java–Installed JREs
5.修改struts.xml中內容
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- devMode爲開發者模式true表示開啓可以不用重啓tomcat也使配置stuts.xml的修改生效-->
<constant name="struts.devMode" value="true" />
<!--namespace爲命名空間,此處值爲"/","/"代表url中"http://localhost/Struts2_0100_Introduction/"部分,其內部的/Hello.jsp標誌表示http://localhost/Struts2_0100_Introduction/Hello.jsp-->
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/Hello.jsp
</result>
</action>
</package>
</struts>
6.將WebRoot下的*.jsp改爲Hello.jsp
7.修改WEB-INF/web.xml 的內容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- "/*"永遠這麼寫,"/"也是代表"http://localhost/Struts2_0100_Introduction/","/*"的意思就是無論"http://localhost/Struts2_0100_Introduction/"後面接的是什麼路徑,都走到這個名爲struts2的filter中,也可以寫*.action,*.action表示所有的action-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8.複製WEB-INF/lib下的所有jar包到自己項目下的WEB-INF的lib文件夾中
9.在對應tomcat上部署服務
- 右鍵項目–Debug As–MyEclipse Server Application–剛自己配置的tomcat
10.在瀏覽器中打開http://localhost:8080/Struts2_0100_Introduction/hello
11.報找不到目錄錯誤
- 檢查上面配置沒有問題 應該是struts的jar包和tomcat版本不符的問題 現我使用的是struts2.3 tomcat8
12.使jar包中.class關聯源碼 即可以正常看到類中的內容而不是編譯後的內容
右鍵項目–properties–Java Build Path–Libraries–點開對應jar包–雙擊Source attachment–External Folder–struts目錄/src/core/src/main/java,此時可以打開jar包中的類看到源碼
右鍵Web App Liberaries下的jar包–properties–Java Source Attachment–External Folder–struts2目錄/src/core/src/main/java
13.看源碼時同時看到javadoc文檔
- 右鍵Web App Liberaries下的jar包–properties–JavaDoc Location–Javadoc URL–Browse–sturts2目錄/soft/struts2.3.1/docs/struts2-core/apidocs,此時在使用該jar包中的類時按F1–javadoc forXXX
14.在xml中敲”<” 沒有關鍵字提示
- 在xml最上查看定義該xml文件語法的文件的位置爲http://struts.apache.org/dtds/struts-2.0.dtd
- Window–Preferences–XML Catalog(Catalog)–add
- key:http://stuts.apache.org/dtds/stuts-2.0.dtd
- key type:URI
- Location:隨便找個stuts2的jar包解壓開裏面有.Dtd文件
15.Struts運行機制
- url–找到指定webapplication(Struts2_0100_Introduction)–讀取其web.xml–匹配url- pattern–找到對應filter類–這個類會查找struts.xml中匹配的namespace–找到匹配的package,找到其內部的action–調用action對應java類的execute方法找到result名–找到其內對應的result,返回result中的內容
16.sturts2的好處
- 提供可擴展性(方便修改),靈活性,將請求與視圖分離(作用)