Struts2和freemarker标签循环list以及获取下标值

合集下载

struts2

struts2

—高级软件人才实作培训专家! 高级软件人才实作培训专家!
搭建Struts2开发环境
搭建Struts2环境时,我们一般需要做以下几个步骤的工作: 1》找到开发Struts2应用需要使用到的jar文件. 2》编写Struts2的配置文件 3》在web.xml中加入Struts2 MVC框架启动配置
北京传智播客教育
北京传智播客教育
—高级软件人才实作培训专家! 高级软件人才实作培训专家!
搭建Struts2开发环境--Struts2在web中的启动配置
在struts1.x中, struts框架是通过Servlet启动的.在struts2中, struts框架是通过Filter启动的.他在web.xml中的配置如下:
—高级软件人才实作培训专家! 高级软件人才实作培训专家!
Struts2
讲师: 讲师:黎活明
北京传智播客教育
—高级软件人才实作培训专家! 高级软件人才实作培训专家!
Struts2
Struts2是在WebWork2基础发展而来的.和struts1一样, Struts2也属于MVC框架. 不过有一点大家需要注意的是:尽管Struts2和struts1在名字上的差别不是很大,但 Struts2和struts1在代码编写风格上几乎是不一样的.那么既然有了struts1,为何还要 推出struts2.主要是因为struts2有以下优点:
北京传智播客教育
—高级软件人才实作培训专家! 高级软件人才实作培训专家!
第一个Struts2应用--HelloWorld
在默认的配置文件struts.xml 中加入如下配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "/dtds/struts-2.0.dtd"> <struts> <package name="itcast" namespace="/test" extends="struts-default"> <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" > <result name="success">/WEB-INF/page/hello.jsp</result> </action> </package> </struts>

Freemarkerlist的简单使用

Freemarkerlist的简单使用

Freemarkerlist的简单使用Freemarker是一种用于Java和Java平台上的模板引擎。

它具有简单易用、快速和灵活的特点,被广泛应用于Web开发中的视图渲染、页面生成和电子邮件模板等场景。

其中,Freemarker提供了对列表(List)的强大支持,可以通过简单的方式实现列表的遍历、分页和过滤等功能。

本文将详细介绍Freemarker中列表的简单使用。

### 1. 引入Freemarker依赖在使用Freemarker之前,需要在项目的构建文件(如Maven的pom.xml或Gradle的build.gradle)中引入Freemarker的依赖。

以下是Maven的示例配置:```xml<dependencies><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.30</version></dependency></dependencies>```### 2. 创建Freemarker配置在使用Freemarker之前,需要创建Freemarker的配置对象,用于指定模板文件的位置和其他相关设置。

如下所示:```javaimport freemarker.template.Configuration;import freemarker.template.TemplateExceptionHandler;// 创建Freemarker配置Configuration cfg = newConfiguration(Configuration.VERSION_2_3_30);cfg.setClassForTemplateLoading(Main.class, "/templates");cfg.setDefaultEncoding("UTF-8");cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RET HROW_HANDLER);```这里的示例代码中,使用`setClassForTemplateLoading(`方法指定模板文件所在的类路径(相对于类的所在位置)。

ftl编辑器(freemarker)的使用和说明

ftl编辑器(freemarker)的使用和说明

一、准备工作1.1 下载文件可以从/站点下载最新的freemarker.jar包(目前最新版本是2.3.18)与eclipse插件(最新版本是0.9.14)。

1.2 安装eclipse插件将下载来的eclipse插件解压缩到本地磁盘,更改hudson.freemarker_ide_0.9.14文件夹中的freemarker-2.3.6.jar文件更改成最新版本的freemarker-2.3.18.jar,并且将META-INF/MANIFEST.MF文件中的Bundle-ClassPath属性值修改为freemarker-2.3.18.jar。

最后将hudson.freemarker_ide_0.9.14放到eclipse的plugins目录中,完成eclipse 插件的安装。

1.3 freemarker文档下载从/官方网站中下载帮助文档(也有中文的手册)二、freemarker的使用2.1 简介2.1.1 FTL tag标签由于freemarker tag标签是属于freemarker的指令,它们仅仅是freemarker 处理过程中的信息,而不会输出到页面上,所以它们与html标签有一些区别。

这些标签都是以#号开头,如<#import "/public/work_public.ftl" as public>。

但用户自定义的标签是以@符号开头,如在freemarker中使用struts标签:<@s.if test="results!=null&&results.size()!=0">注意FTL标签不可以在其他FTL标签和插值中使用(注释可以在FTL标签和插值中使用,因为它在解析时,整个注释都将抛弃,不会影响具体代码),如下面这样就是一个错误的写法:<#if <#include 'foo'>='bar'>...</#if>2.1.2 注释freemarker的文档注释与html相似,但它是以<#--和-->来分割的。

遍历list的几种方式

遍历list的几种方式

遍历list的几种方式遍历list是在编程中常见的操作之一,能够帮助我们访问列表中的每个元素。

在这篇文章中,我们将详细介绍几种常见的遍历list的方式,并对每种方式进行逐一分析。

首先,我们将从最基础的方式开始,逐渐深入研究每种遍历list的方式的原理和使用场景。

1. 使用for循环遍历listfor循环是一种简单而常见的遍历list的方式。

它的语法结构如下:for item in my_list:# 在这里进行操作这个循环会依次将列表中的每个元素赋给变量`item`,然后执行循环体中的操作。

我们可以在循环体中对每个元素进行特定的操作,比如打印元素的值或者对元素进行计算。

for循环的优点是简洁明了,且可以直接操作list中的每个元素。

然而,它并不能提供索引位置的信息,有时可能需要使用其他方法。

2. 使用while循环和索引遍历list当我们需要获取元素的索引位置时,可以使用while循环结合索引进行遍历。

以下是实现该方式的一种方法:index = 0while index < len(my_list):item = my_list[index]# 在这里进行操作index += 1这个循环会在遍历过程中不断增加索引`index`的值,从而逐一获取list 中的元素并执行相应的操作。

由于我们显式地操作索引,因此可以根据需要灵活地控制循环逻辑。

这种遍历方式特别适用于需要根据元素的索引位置执行特定操作的情况。

例如,我们可以根据索引位置筛选出特定范围的元素进行处理。

3. 使用enumerate函数遍历list如果想在for循环中同时获得索引位置和元素值,可以使用`enumerate()`函数。

这个函数能够返回一个包含索引位置和元素值的元组,从而实现同时获取两者的目的。

以下是使用`enumerate()`函数遍历list的示例代码:for index, item in enumerate(my_list):# 在这里进行操作这种方式可以简化代码,同时提供了索引位置和元素值的信息,使程序更易读且更具可读性。

FreeMarker指令

FreeMarker指令

FreeMarker指令FreeMarker指令的组成:* 文本:直接输出的部分。

* 注释:即<#--...-->格式部分,不会输出。

* 插值:即${...}或#{...}格式的部分,将使用数据模型中的部分替代输出。

* FTL指令:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出。

1>.FTL指令规则:开始标签:<#directivename parameters>结束标签:</#directivename>空标签:<#directivename parameters/>注:如果某指令是一个用户自定义指令时,应将#符合改为@符合。

2>.插值规则:通用插值:${expr}数字格式化插值:#{expr}或#{expr,format}1>.介绍通用插值:${expr}* 插值结果为字符串值:直接输出表达式结果.* 插值结果为数字值:根据默认格式(由#setting指令设置)将表达式结果转换成文本输出。

* 插值结果为日前值:根据默认格式(由#setting指令设置)将表达式结果转换成文本输出。

* 插值结果为布尔值:根据默认格式(由#setting指令设置)将表达式结果转换成文本输出。

例子1:<#setting number_format="currency"/><#assign answer=42/> //输出结果${answer} //$42.00${answer?string} //$42.00${answer?string.number} //42${answer?string.currency}//$42.00${answer?string.percent}//4,200%例子2:<#assign lastUpdate=42/>${lastUpdate?string("yyyy-MM-dd HH:mm:ss zzzz")}${lastUpdate?string("EEE,MMM d, ''yy")}${.................("EEEE,MMM dd,yyyy, hh:mm:ss a '('zzz')'")}输出结果如下:2003-04-08 21:24:44 Pacific Daylight TimeTue,Apr 8,'03Tuesday,April 08,2003,09:24:44 PM (PDT)例子3:<#assign foo=true/>${foo?string("yes","no")} //输出结果:yes2>介绍数字化插值mX:小数部分最小X位。

struts2

struts2

/topic/128973struts2表单标签1、struts资源文件中文解决方法在命令行中输入以下命令native2ascii -encoding gb2312 ApplicationResources.properties ApplicationResourcesgbk.properties第一个文件为资源文件,第二个为新生成的文件,将新生成的文件拷入工程就可以了2、开发struts2流程1.创建web工程2.拷贝commons-fileupload-1.2.1.jar,commons-logging-1.0.4.jar,freemarker-2.3.13.jar,ognl-2.6.11.jar,struts2-core-2.1.6.jar,xwork-2.1.2.jar,放入web-inf--lib中3.在web.xml文件中添加struts2的核心Filter,过滤客户端发送过来的请求,代码如下<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>org.apache.struts2.dispatcher.FilterDispatcher为struts2中提供的一个控制器4.创建struts.xml文件,放在src目录下.代码如下:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""/dtds/struts-2.0.dtd"><struts><package name="strutsqs" extends="struts-default"></package></struts>5.在src目录下创建一个类,创建几个属性及对应的get,set方法6.创建public String execute() throws Exception方法,7.在struts.xml文件中添加如下代码:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""/dtds/struts-2.0.dtd"><struts><package name="strutsqs" extends="struts-default"><action name="login" class="ers.action.LoginAction"><result name="success">/Main.jsp</result><result name="failed">/Login.jsp</result></action></package></struts>3、创建拦截器:1.创建类继承AbstractInterceptor,覆盖intercept方法,代码如下public String intercept(ActionInvocation invocation) throws Exception {LoginAction action=(LoginAction)invocation.getAction();String result=invocation.invoke();return null;}2.在struts.xml文件中配置如下<package name="strutsqs" extends="struts-default"><interceptors><interceptor name="myInterceptor" class="ers.interceptor.LoginInterceptor"> <param name="name">简单拦截器</param></interceptor></interceptors><action name="login" class="ers.action.LoginAction"><result name="success">/Main.jsp</result><result name="failed">/Login.jsp</result><result name="input">/Login.jsp</result><interceptor-ref name="myInterceptor"></interceptor-ref></action></package>也可以创建拦截器栈<interceptors><interceptor name="myinterceptor1"class="com.haitian.bbs.interceptor.MyInterceptor"></interceptor> <interceptor name="myinterceptor2"class="com.haitian.bbs.interceptor.MyInterceptor2"></interceptor> <interceptor-stack name="stack1"><interceptor-ref name="myinterceptor2"></interceptor-ref><interceptor-ref name="myinterceptor1"></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors>4.标签中引用资源文件内容<s:textfield name="age" key="regist.age" value="%{getText('regist.password')}"></s:textfield><s:select list=”#{‘1’:getText(‘sex.man’),’0’:getText(‘sex.women’)}”<s:text name=”bel”/>5.创建资源文件在src下创建messageResource_zh_CN.properties文件在src下创建struts.properties文件,添加struts.custom.i18n.resources=messageResource_zh_CN 6.Action中访问servlet APIActionContext ctx=ActionContext.getContext();ctx.getApplication()获得application对象ctx.put(“”,””);//相当于request.setAttribute(“”,””);ctx.getSession()//获得session对象get(Object key);//类似于调用HttpServletRequest的getAttribute(String name);方法Map getApplication();返回一个Map对象,该对象模拟了应该的ServletContext实例Map getSession();返回一个Map对象,该Map对象模拟了HttmpSession实例Map getParameters();获取所有的请求参数,类似于调用HttpServletRequest对象的getParameterMap方法Void setSession(Map session);直接传入一个Map实例,将实例里的key-value对转换成session的属性名\属性值HttpServletRequest request=ServletActionContext.getRequest();7.Action访问资源文件Action 继承ActionSupport调用this.getText(“ername”);Jsp访问资源文件<s:text name=”bel”/>8.资源文件_各国代码资源文件命名格式:baseName_language_country.propertiesLocale[] localeList=Locale.getAvailableLocales();for(int i=0;i<localeList.length;i++){System.out.println(localeList[i].getDisplayCountry()+”=”+localeList[i].getCountry()+””+localeList[i ].getDisplayLanguage()+”=”+localeList[i].getLanguage());}9.将struts.xml分开在struts.xml中添加如下代码,将其它配置文件导入<struts><include file=”struts-part1.xml”/>..</struts>被包含的配置文件是标准的struts2配置文件,一样包含dtd 信息、strut2配置文件的根元素,此时struts.xml文件中不能再定义action10全局结果result<global-results><result name="">/te.jsp</result></global-results>11模型驱动1创建一个JavaBean,生成setter,getter方法2创建action类实现ModelDriven接口12.创建拦截器1创建类继承AbstractInterceptor,覆盖intercept(ActionInvocation invocation)方法2. ActionContext ac=arg0.getInvocationContext();Map session=ac.getSession();获得session对象Object obj=session.get(“CURRENTUSERBEAN”);//获得session中是否存在此对象If(obj==null)没有登陆过Return “login”;ElseReturn invocation.invoke();3.LoginAction la=(LoginAction)invocation.getAction();获得被拦截的Action 实例4.String result=invocation.invoke();调用execute方法,将拦截方法返回的字符串做为拦截器的返回值或者返回其它字符串,此字符串与struts.xml文件中的result标签中的结果对应。

struts2中struts.xml文件详解

struts2中struts.xml文件详解

<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
</action>
</package>
</struts>
上面代码的两个动作的class属性都指向同一个类,name为这个类起了两个动作别名:test和my。在动作my中,使用了method属性指定要要运行的方法名为my。
在MyAction类中必须要有my方法,代码如下:
<struts>
<package name="demo" extends="struts-default"
>
<action name="test" class="action.MyAction">
</action>
<action name="my" class="action. MyAction" method="my">
<?xml version="1.0" encoding="UTF-8" ?>

Apache Struts 2 教程(PDF)说明书

Apache Struts 2 教程(PDF)说明书

About the T utorialApache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. This framework is designed to streamline the full development cycle from building, to deploying and maintaining applications over time. Apache Struts 2 was originally known as Web Work 2.This tutorial will teach you, how to use Apache Struts for creating enterprise-ready Java web applications in simple and easy steps.AudienceThis tutorial is designed for Java programmers who are interested to learn the basics of Struts 2.x framework and its applications.PrerequisitesBefore proceeding with this tutorial, you should have a good understanding of the Java programming language. A basic understanding of MVC Framework and JSP or Servlet is very helpful.Disclaimer & CopyrightCopyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute, or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness, or completeness of our website or its contents including this tutorial. If you discover any errorsonourwebsiteorinthistutorial,******************************************iT able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Disclaimer & Copyright (i)Table of Contents .................................................................................................................................. i i STRUTS 2 - BASIC MVC ARCHITECTURE (1)The Model (2)The View (2)The Controller (2)STRUT 2 – OVERVIEW (3)Struts 2 Framework Features (3)Struts 2 Disadvantages (4)ENVIRONMENT SETUP (5)Step 1 - Setup Java Development Kit (JDK) (5)Step 2 - Setup Apache Tomcat (5)Step 3 - Setup Eclipse (IDE) (7)Step 4 - Setup Struts2 Libraries (8)STRUTS 2 – ARCHITECTURE (10)Request Life Cycle (11)STRUTS 2 – EXAMPLES (12)Create a Dynamic Web Project (13)Create Action Class (14)Create a View (15)Create Main Page (16)iiTo Enable Detailed Log (18)Procedure for Executing the Application (19)STRUTS 2 – CONFIGURATION (22)The web.xml File (22)The Struts.xml File (23)The Struts-config.xml File (25)The Struts.properties File (27)STRUTS 2 – ACTIONS (29)Create Action (29)Create a View (31)Execute the Application (33)Create Multiple Actions (35)STRUTS 2 – INTERCEPTORS (37)Struts 2 Framework Interceptors (37)How to Use Interceptors? (39)Create Custom Interceptors (40)Create Interceptor Class (41)Create Action Class (42)Create a View (43)Create Main Page (43)Configuration Files (44)Stacking Multiple Interceptors (46)STRUTS 2 – RESULT TYPES (48)The Dispatcher Result Type (48)iiiThe Redirect Result Type (50)STRUTS 2 – VALUE STACK / OGNL (52)The Value Stack (52)The OGNL (53)ValueStack/OGNL Example (54)Create Views (56)Configuration Files (57)STRUTS 2 – FILE UPLOADS (60)Create View Files (60)Create Action Class (61)Configuration Files (63)Error Messages (67)STRUTS 2 – DATABASE ACCESS (69)Create Action (69)Create Main Page (71)Create Views (72)Configuration Files (73)STRUTS 2 – SENDING EMAIL (77)Create Action (77)Create Main Page (80)Create Views (81)Configuration Files (82)STRUTS 2 - VALIDATIONS (86)Create Main Page (86)ivCreate Action (87)Configuration Files (88)How this Validation Works? (91)Xml Based Validation (92)STRUTS 2 - LOCALIZATION (95)Resource Bundles (95)Access the messages (96)Localization Example (96)STRUTS 2 – TYPE CONVERTION (104)STRUTS 2 – THEMES & TEMPLATES (110)Selecting Themes (111)How a Theme Works? (112)Creating New Themes (112)STRUTS 2 – EXCEPTION HANDLING (115)Global Exception Mappings (120)STRUTS 2 – ANNOTATIONS (122)Create Main Page (122)Create Views (123)Create Action (124)Configuration Files (125)STRUTS 2 – CONTROL TAGS (130)The If and Else Tags (130)If and Else Tags – Detailed Example (130)The Iterator Tags (134)vThe Iterator Tags – Detailed Example (135)The Merge Tag (142)The Merge Tag – Detailed Example (142)The Append Tag (147)The Append Tag – Detailed Example (148)The Generator Tag (153)The Generator Tag – Detailed Example (154)STRUTS 2 – DATA TAGS (158)The Action Tag (158)The Action Tag – Detailed Example (158)The Include Tag (164)The Include Tag – Detailed Example (164)The Bean Tag (168)The Bean Tag – Detailed Example (169)The Date Tag (173)The Date Tag - Detailed Example (173)The Param Tag (176)The Param Tag – Detailed Example (177)The Property Tag (181)The Property Tag – Detailed Example (181)The Push Tag (185)The Push Tag – Detailed Example (186)The Set Tag (190)The Set Tag – Detailed Example (190)The Text Tag (195)The Text Tag – Detailed Example (195)viThe URLTag (199)STRUTS 2 – THE FORM TAGS (204)Simple UI Tags (204)Group UI Tags (206)STRUTS 2 – THE AJAX TAGS (211)STRUTS 2 & SPRING INTEGRATION (214)STRUTS 2 & TILES INTEGRATION (220)STRUTS 2 & HIBERNATE (226)Database Setup (226)Hibernate Configuration (226)Envrionment Setup (227)Hibernate Classes (228)Action Class (231)Struts Configuration (234)viiStruts 2 1M odel V iew C ontroller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts:∙Model - The lowest level of the pattern which is responsible for maintaining data. ∙View - This is responsible for displaying all or a portion of the data to the user. ∙ Controller - Software Code that controls the interactions between the Model and View. MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows.STRUTS 2 - BASIC MVC ARCHITECTUREStruts 2The ModelThe model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to update itself.The ViewIt means presentation of data in a particular format, triggered by a controller's decision to present the data. They are script-based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX technology.The ControllerThe controller is responsible for responding to the user input and perform interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model.Struts2 is a MVC based framework. In the coming chapters, let us see how we can use the MVC methodology within Struts2.2Struts 2 3 Struts2 is a popular and mature web application framework based on the MVC design pattern. Struts2 is not just a new version of Struts 1, but it is a complete rewrite of the Struts architecture.The Webwork framework initially started with Struts framework as the basis and its goal was to offer an enhanced and improved framework built on Struts to make web development easier for the developers.After a while, the Webwork framework and the Struts community joined hands to create the famous Struts2 framework.Struts 2 Framework FeaturesHere are some of the great features that may force you to consider Struts2:∙ Pojo Forms and Pojo Actions - Struts2 has done away with the Action Forms that were an integral part of the Struts framework. With Struts2, you can use any POJO to receive the form input. Similarly, you can now see any POJO as an Action class.∙ Tag Support - Struts2 has improved the form tags and the new tags which allow the developers to write less code.∙ Ajax Support - Struts2 has recognized the take over by Web2.0 technologies, and has integrated AJAX support into the product by creating AJAX tags, this function is very similar to the standard Struts2 tags.∙ Easy Integration - Integration with other frameworks like Spring, Tiles and SiteMesh is now easier with a variety of integration available with Struts2.∙ Template Support - Support for generating views using templates.∙ Plugin Support - The core Struts2 behavior can be enhanced and augmented by the use of plugins. A number of plugins are available for Struts2.∙ Profiling - Struts2 offers integrated profiling to debug and profile the application. In addition to this, Struts also offers integrated debugging with the help of built in debugging tools.∙ Easy To Modify Tags - Tag markups in Struts2 can be tweaked using Freemarker templates. This does not require JSP or java knowledge. Basic HTML, XML and CSS knowledge is enough to modify the tags.∙Promote Less Configuration - Struts2 promotes less configuration with the help of using default values for various settings. You don't have to configure something unless it deviates from the default settings set by Struts2.STRUT 2 – OVERVIEWStruts 2 ∙View Technologies- Struts2 has a great support for multiple view options (JSP, Freemarker, Velocity and XSLT)Listed above are the Top 10 features of Struts 2 which makes it as an Enterprise ready framework.Struts 2 DisadvantagesThough Struts 2 comes with a list of great features, there are some limitations of the current version - Struts 2 which needs further improvement. Listed are some of the main points:∙Bigger Learning Curve- To use MVC with Struts, you have to be comfortable with the standard JSP, Servlet APIs and a large & elaborate framework.∙Poor Documentation- Compared to the standard servlet and JSP APIs, Struts has fewer online resources, and many first-time users find the online Apache documentation confusing and poorly organized.∙Less Transparent - With Struts applications, there is a lot more going on behind the scenes than with normal Java-based Web applications which makes it difficult to understand the framework.Final note, a good framework should provide generic behavior that many different types of applications can make use of it.Struts 2 is one of the best web frameworks and being highly used for the development of Rich Internet Applications (RIA).4Struts 2 5Our first task is to get a minimal Struts 2 application running. This chapter will guide you on how to prepare a development environment to start your work with Struts 2. I assume that you already have JDK (5+), Tomcat and Eclipse installed on your machine. If you do not have these components installed, then follow the given steps on fast track: Step 1 - Setup Java Development Kit (JDK)You can download the latest version of SDK from Oracle's Java site: Java SE Downloads . You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally, set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.If you are running Windows and installed the SDK in C:\jdk1.5.0_20, you should be inputting the following line in your C:\autoexec.bat file.Alternatively, on Windows NT/2000/XP:∙ You can right-click on My Computer, Select Properties, then Advanced, then Environment Variables. Then, you would update the PATH value and press the OK button.∙On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.5.0_20 and you use the C shell, you would put the following into your .cshrc file. Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, compile and run a simple program to confirm that the IDE knows where you installed Java, otherwise do proper setup as per the given document of IDE.Step 2 - Setup Apache T omcatYou can download the latest version of Tomcat from /. Once you downloaded the installation, unpack the binary distribution into a convenient location. For example in C:\apache-tomcat-6.0.33 on windows, or /usr/local/apache-tomcat-6.0.33 on Linux/Unix and create CATALINA_HOME environment variable pointing to these locations.ENVIRONMENT SETUPYou can start Tomcat by executing the following commands on windows machine, or you can simply double click on startup.batTomcat can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine:After a successful startup, the default web applications included with Tomcat will be available by visiting http://localhost:8080/. If everything is fine, then it should display the following result:Further information about configuring and running Tomcat can be found in the documentation included here, as well as on the Tomcat website: 67 Tomcat can be stopped by executing the following commands on windows machine:Tomcat can be stopped by executing the following commands on Unix (Solaris, Linux, etc.) machine:Step 3 - Setup Eclipse (IDE)All the examples in this tutorial are written using Eclipse IDE. I suggest that, you have the latest version of Eclipse installed in your machine.To install EclipseDownload the latest Eclipse binaries from /downloads/. Once you download the installation, unpack thebinary distribution into a convenient location.For example in C:\eclipse on windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on windows machine, or you can simply double click on eclipse.exeEclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine:After a successful startup, if everything is fine, it should display the following result:Step 4 - Setup Struts2 LibrariesNow if everything is fine, then you can proceed to setup your Struts2 framemwork. Following are the simple steps to download and install Struts2 on your machine.∙Make a choice whether you want to install Struts2 on Windows, or Unix and then proceed to the next step to download .zip file for windows and .tz file for Unix.∙Download the latest version of Struts2 binaries from /download.cgi.∙At the time of writing this tutorial, I downloaded struts-2.0.14-all.zip and when you unzip the downloaded file it will give you directory structure inside C:\struts-2.2.3 as follows.8Second step is to extract the zip file in any location, I downloaded & extracted struts-2.2.3-all.zip in c:\folder on my Windows 7 machine so that I have all the jar files into C:\struts-2.2.3\lib. Make sure you set your CLASSPATH variable properly otherwise you will face problem while running your application.9End of ebook previewIf you liked what you saw…Buy it from our store @ https://10。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档