`
雪馨25
  • 浏览: 128405 次
  • 性别: 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入门教程-分析及讲解

    除了这个入门教程,还有许多官方文档、在线教程和社区论坛可以提供帮助,如Apache Struts官网、Stack Overflow等,遇到问题可以去这些地方寻求解答。 通过本教程的学习,你将能够理解和使用Struts2框架搭建web应用...

    struts2+hibernate+spring

    1. **Struts2**:Struts2是一个基于MVC设计模式的Web框架,它处理用户请求并将其映射到特定的Action,然后执行相应的业务逻辑。在本项目中,Struts2负责接收前端请求,根据配置的Action映射规则转发到对应的处理方法...

    Struts2+Hibernate+Spring整合后的增删改查

    在"Struts2+Hibernate+Spring整合后的增删改查"项目中,开发者已经完成了一个基础的CRUD(创建、读取、更新和删除)功能示例,这对于初学者来说是非常有价值的实践案例。以下是对该项目中涉及的知识点的详细解释: ...

    displytag 学习与参考和源文件

    displytag 相信大家都听说过吧. 与Struts结合使用最出名的一个tag主要是显示表格数据很漂亮、完善。和DataGrid功能一样强大. 此资源包括三个包,有官方的,可以跟着学习,不过是英文的(display tag学习);有...

    JAVA自学之路

    所以,动手吧,跟着做,一行一行的跟着敲,一个项目敲完了,比如坦克,再试着加一些自己的功能,按照自己的思路敲一些代码,收获远比干听大的多。 如果遇到,暂时对于一些思路不能理解的同学,我一般都会劝告说,...

    BBS下载(毕业设计迪哥)

    JSP的优势在于可以与Java的后端框架如Spring、Struts等无缝集成,提供强大的功能和良好的可维护性。 3. **MySQL**:MySQL是一种流行的关系型数据库管理系统(RDBMS),广泛应用于Web应用中。在BBS系统中,MySQL用于...

    SSH搭建 视频教程 简单 Java 框架

    SSH(Struts + Spring + Hibernate)是Java Web开发中一种常见的技术栈,它结合了Struts的MVC架构、Spring的依赖注入与事务管理以及Hibernate的对象关系映射工具,为开发者提供了一种高效且灵活的开发环境。...

    韩顺平.SSH框架视频教程-项目实战-校内网(含源代码、设计文档、关系图和数据库脚本)

    【SSH框架】是Java开发中的一个经典组合,由Spring、Struts和Hibernate三个开源框架集成。这个"韩顺平.SSH框架视频教程-项目实战-校内网"提供了一个全面学习SSH框架实操的资源,适合Java Web开发者,尤其是初学者...

    北风客户关系管理源码 CRM

    跟着视频做的 ,代码原来老师教程视频用的拼音简写字段 和表名 我是把字段都翻译了,自己重做一遍的,我做这个也是感觉,没有代码,一个字母敲挺郁闷的,尤其那个html页面。 说明一点:把数据库导进去后,把端口改好...

    SSH集成录像

    2. **创建项目**:在MyEclipse中创建一个新的Web项目,然后添加Struts、Hibernate和Spring的jar包到项目的类路径中。 3. **配置Struts**:编写struts-config.xml文件,定义Action和ActionForm,设置控制器的路由...

    java开发erp源码-NSFW_Systen:学SSH时写的纳税服务系统,详情可看ReadMe

    Struts2,Spring,Hibernate JSP Javascript,jQuery,AJAX 某些工具类及组件如(FileUtil,POI,DateUtils) JS组件:日期组件、富文本编辑器、Fusionchart(图表组件) 抽取Action、Service、Dao,全局异常处理 权限控制...

Global site tag (gtag.js) - Google Analytics