跟着官網學Struts2使用(一)

跟着官網學,提升閱讀能力,系統學習

你是不是和我一樣?

1、開發過程中,必然要學習新的軟件、框架、工具等等
2、大多數的官網都是英文的,看着都頭大不願繼續學習
3、在網上四零八落地蒐集相關知識卻又不夠全面,不夠系統
4、想要跟着官網學,又找不到合適的方式

爲了改善我自己懼怕官網的癥結,我問了朋友,多次嘗試,終於是開始有所好轉。決定記錄爲博客,便於回憶翻閱,以及知識總結

告訴自己,英語並不難

一、前期準備

爲什麼選Struts2
首先,爲了避免生詞阻礙閱讀,安裝一個英語即時翻譯軟件,事實證明鼠標放在單詞上就可以翻譯幫了我很大忙
其次,選擇Struts2是因爲我對她有一些應用,不算是一無所知,理解起來會有幫助
再者,Struts很常用,有紮實的基礎是必須的
前期準備

首先要確定會簡單的使用maven,tomcat
確認Struts2的版本:struts-2.3.1.1
本次目標:把Struts2添加進web應用
鎖定:Start  或者  Quick Start或者How to use或者Tutorials 諸如此類的單詞,以便快速開始

二、學習過程記錄

官網:http://struts.apache.org/2.2.1/index.html
瀏覽官網 看到Get Started 點擊進入,瀏覽看到Tutorials
瀏覽發現 ---->Getting Started - Tutorials For Struts 2 Beginners  還等什麼?就是他了

Q: 也許你會猶豫爲什麼不是“Struts 2 Tutorials”?
A: 無妨,點進去瀏覽下,原來他是一些具體的應用方式的教程,而現在我要學的是最簡單的helloWorld

How To Create A Struts 2 Web Application---->Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application

步驟(以下都是理解,不是直譯):

Step 1 -- create a Java web application with a project name of Basic_Struts2_Mvn that follows the standard Maven project folder structure.(用maven創建一個名爲Basic_Struts2_Mvn的web項目)
開始----運行----cmd---至項目需要創建的路徑(這是maven的基礎知識,如果你不會,就去看我的博客關於maven的介紹吧)
mvn archetype:create
 -DgroupId=rwq.com
 -DartifactId=Basic_Struts2_Mvn
 -Dpackage=com.rwq.Basic.Struts2.Mvn
 -DarchetypeArtifactId=maven-archetype-webapp
 -Dversion=1.0-SNAPSHOT

Step 2 --Add index.jsp
Create an index.jsp under src/main/webapp with a title of "Basic Struts 2 Application - Welcome" and in the body add an h1 heading of "Welcome to Struts 2!"(在項目所在路徑下的src/main/webapp目錄下創建index.jsp文件,內容爲 h1 標籤下 "Welcome to Struts 2!")
Run mvn clean package to create the war file. Copy the war file into your Servlet container so that it will deploy the war file.(命令行跳轉至項目所在路徑運行mvn clean package用以創建war包,複製war包至servlet容器,提示war包在項目所在路徑下的target文件夾


如我的在:D:\Project\mavenProject\Basic_Struts2_Mvn\target目錄下的Basic_Struts2_Mvn.war

Step 3 -- 運行tomcat,確認war包可以正常發佈

Start up your Servlet container and in a web browser go to http://localhost:8080/Basic_Struts2_Mvn/index.jsp. You should see the following:

Step 4 --Add Struts 2 Jar Files To Class Path

Now that we know we have a working Java web application, lets add the minimal required Struts 2 framework Jar files to our web application's class path. In pom.xml add the following dependency node:(現在我們有一個運行中的WEB應用。先將Struts 2框架的jar文件加入項目的class path,對maven項目只需要在pom.xml文件加入如下信息,我需要將X.X.X.X換爲2.3.1.1)
<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-core</artifactId>
	<version>X.X.X.X</version>
</dependency>
<dependency>
	<groupId>javassist</groupId>
	<artifactId>javassist</artifactId>
	<version>3.8.0.GA</version>
</dependency>
Of course replace the X.X.X.X with the current Struts 2 version. Note: beginning with version 2.2.1 of Struts 2 you must have the javassist jar on your class path.

Step 5 - Add Logging(pom.xml中添加log4j)
To see what's happening under the hood, the example application for this tutorial uses log4j. You'll need to add to pom.xml a dependency node for the log4j jar file:
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.14</version>
</dependency>

Step 6-- Setup a log4j.xml configuration in the src/main/resources folder(在項目所在路徑下的src/main/resources文件夾建立log4j.xml文件,內容如下)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
       <layout class="org.apache.log4j.PatternLayout"> 
          <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/> 
       </layout> 
    </appender>
 
    <!-- specify the logging level for loggers from other libraries -->
    <logger name="com.opensymphony">
    	<level value="DEBUG" />
    </logger>

    <logger name="org.apache.struts2">
    	 <level value="DEBUG" />
    </logger>
  
   <!-- for all other loggers log only info and above log messages -->
     <root>
        <priority value="INFO"/> 
        <appender-ref ref="STDOUT" /> 
     </root> 
</log4j:configuration> 

Step 7 - Add Struts 2 Servlet Filter
To enable the Struts 2 framework to work with your web application you need to add a Servlet filter class and filter mapping to web.xml. Below is the filter and filter-mapping nodes you should add to web.xml.(web.xml.中添加struts2過濾器,內容如下)
<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>
   <url-pattern>/*</url-pattern>
</filter-mapping>

Step 8 - Create struts.xml
Note the file name is struts.xml and it should be in the src/main/resources folder (struts.xml must be on the web application's root class path).(在項目所在路徑下的src/main/resources文件夾建立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>
	<constant name="struts.devMode" value="true" />
	<package name="basicstruts2" extends="struts-default">
		<action name="index">
			<result>/index.jsp</result>
		</action>
	</package>
</struts>

Step 9 - Build and Run the Application
run mvn clean package to create the war file.
Copy to your Servlet container's webapps folder the new war you just created.
Start up the Servlet container.
Open a web browser and go to http://localhost:8080/Basic_Struts2_Mvn/index.action (note that's index.action not index.jsp at the end of the URL).

(命令行再次運行mvn clean package,將建立好的war包發佈到tomcat中,這一次瀏覽http://localhost:8080/Basic_Struts2_Mvn/index.action 注意不是index.jsp,會見到如下界面)

至此,項目天際Struts2成功了!你會了嗎?

其實官網真的不難,貴在你願意看

發佈了90 篇原創文章 · 獲贊 107 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章