跟着官网学,提升阅读能力,系统学习
你是不是和我一样?
1、开发过程中,必然要学习新的软件、框架、工具等等
2、大多数的官网都是英文的,看着都头大不愿继续学习
3、在网上四零八落地搜集相关知识却又不够全面,不够系统
4、想要跟着官网学,又找不到合适的方式
为了改善我自己惧怕官网的症结,我问了朋友,多次尝试,终于是开始有所好转。决定记录为博客,便于回忆翻阅,以及知识总结
告诉自己,英语并不难
一、前期准备
为什么选Struts2
首先,为了避免生词阻碍阅读,安装一个英语即时翻译软件,事实证明鼠标放在单词上就可以翻译帮了我很大忙
其次,选择Struts2是因为我对她有一些应用,不算是一无所知,理解起来会有帮助
再者,Struts很常用,有扎实的基础是必须的
前期准备
首先要确定会简单的使用maven,tomcat
确认Struts2的版本:struts-2.3.1.1
本次目标:把Struts2添加进web应用
锁定:Start 或者 Quick Start或者How to use或者Tutorials 诸如此类的单词,以便快速开始
二、学习过程记录
浏览官网 看到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
步骤(以下都是理解,不是直译):
开始----运行----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.xmlNote 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成功了!你会了吗?
其实官网真的不难,贵在你愿意看