`
雪馨25
  • 浏览: 125510 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

跟着官网学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成功了!你会了吗?

其实官网真的不难,贵在你愿意看

0
0
分享到:
评论
5 楼 mr_wangjq 2013-01-28  
4 楼 雪馨25 2013-01-28  
sweat89 写道
嗯,楼主的学习方法,很值得我学习。加油!!

一起加油吧 
3 楼 雪馨25 2013-01-28  
381895649 写道
建议用用springmvc,哈.

嗯,都是必备知识,都要学
2 楼 sweat89 2013-01-27  
嗯,楼主的学习方法,很值得我学习。加油!!
1 楼 381895649 2013-01-27  
建议用用springmvc,哈.

相关推荐

    struts2帮助文档

    struts2的概述struts2下的HelloWord struts2基础 struts2做好准备 struts2的标志 struts2的Action讲解 struts2中的国际化 struts2转化器 struts2实现表单数据校验 struts2的基石-拦截器 struts2中实现IOC struts2中...

    struts2标签使用例子

    struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子

    Struts2视频教程

    Struts2是一套非常优秀的Java WEB应用框架,实现优雅、功能强大、使用简洁。目前已有大量的企业项目采用Struts2来作为Web框架进行开发,掌握Struts2是每个进行Web开发的Java程序员的一项必备技能。 本视频对Struts2...

    struts1和struts2的区别

    struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别...

    struts2学习测试代码,struts2学习测试代码2

    struts2学习测试代码,struts2学习测试代码2struts2学习测试代码,struts2学习测试代码

    struts2 学习资料

    本人在网上收集的struts2 的学习资料,包括:struts2权威指南、struts2标签简单说明、struts2中文教程。

    struts2(1-7)源码 struts2学习入门 源码学习

    struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter struts5 增加集合类型,使用范型 struts6 直接设置要进行类型转换的字段值 struts7 制作一个表单,对表单中的内容进行...

    struts2中文学习文档

    struts2的根本webwork2

    struts2案例 struts2 struts2源码

    struts2源码 struts2案例 struts2使用包

    STRUTS2站点,STRUTS2学习实例8

    STRUTS2站点,STRUTS2学习实例8

    Struts2使用详情

    关于Struts2的使用详情,以及绝对路径和相对路径的使用

    struts2使用手册

    struts2使用手册

    struts2jar包

    使用struts2开发之前必须引入struts的包。

    STRUTS 2完全学习手册.pdf

    《Struts 2完全学习手册》全面讲述Struts 2开发框架的知识,包括Struts 2中配置文件的详细讲解、国际化与异常处理、数据类型的转换、数据的输入...,最后以3个实例由简单到复杂地介绍Struts 2在实际应用中的使用...

    struts2初始使用环境配置

    javaEE学习阶段,struts2初步使用环境配置。 javaEE学习阶段,struts2初步使用环境配置。

    struts2 资料struts2 资料struts2 资料struts2 资料

    struts2 资料struts2 资料struts2 资料struts2 资料

    struts2学习测试代码

    struts2学习测struts2学习测试代码试代码

    struts2 总结工程大全

    struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...

    Struts2初步使用总结

    Struts2初步使用总结 初步介绍Struts2和用法

Global site tag (gtag.js) - Google Analytics