`
andyjames
  • 浏览: 31021 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

struts2示例学习1-HelloWorld

阅读更多

      这是Struts2的第二篇入门学习,从helloworld入手学习,使用了MVC的设置模式,下面是详细介绍和示例:

    当提交一个Html的Form给Struts2框架时,数据不再是提交给服务器端的某一个JSP页面,而是提交给一个Action类。而框架根据配置文件把与该Action类对应的页面(这个页面可以是JSP页面,也可以是PDF、Excel或Applet)返回给客户端。

写一个Struts2的HelloWorld , 我们需要做三件事:

1. 创建一个显示信息的JSP文件

2. 创建一个生成信息的Action类

3. 建立JSP页面和Action的mapping(映射)

创建HelloWorld.jsp文件 
 

<!----><%...@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World!</title> </head> <body> <h2><s:property value="message" /></h2> </body> </html>


HelloWorld.jsp存放在war目录下面

创建Action类HelloWorld.java
 

<!---->package tutorial; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport ...{ public static final String MESSAGE = "Struts is up and running ..."; public String execute() throws Exception ...{ setMessage(MESSAGE); return SUCCESS; } private String message; public void setMessage(String message)...{ this.message = message; } public String getMessage() ...{ return message; } }

HelloWorld.java存放在src/tutorial下面
在struts.xml建立映射

<!----><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="tutorial" extends="struts-default"> <action name="HelloWorld" class="tutorial.HelloWorld"> <result>/HelloWorld.jsp</result> </action> <!-- Add your actions here --> </package> </struts>

 
    此文件存放在classes下面,同时还要建一个struts.properties的属性文件放在这个目录下,这个文件可以是空的,什么都不写
创建web.xml

<!----><?xml version="1.0" encoding="UTF-8"?> <web-app> <display-name>Struts2 Hello World!</display-name> <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> </web-app>

 
web.xml毫无疑问放在WEB-INF下

 
创建build.xml

<!----><?xml version="1.0"?> <project name="struts2app" basedir="." default="usage"> <property file="build.properties"/> <property name="src.dir" value="src"/> <property name="web.dir" value="war"/> <property name="build.dir" value="${web.dir}/WEB-INF/classes"/> <property name="name" value="struts2app"/> <path id="master-classpath"> <fileset dir="${web.dir}/WEB-INF/lib"> <include name="*.jar"/> </fileset> <!-- We need the servlet API classes: --> <!-- for Tomcat 4.1 use servlet.jar --> <!-- for Tomcat 5.0 use servlet-api.jar --> <!-- for Other app server - check the docs --> <fileset dir="${appserver.home}/common/lib"> <include name="servlet*.jar"/> </fileset> <pathelement path="${build.dir}"/> </path> <target name="usage"> <echo message=""/> <echo message="${name} build file"/> <echo message="-----------------------------------"/> <echo message=""/> <echo message="Available targets are:"/> <echo message=""/> <echo message="build --> Build the application"/> <echo message="deploy --> Deploy application as directory"/> <echo message=""/> </target> <target name="build" description="Compile main source tree java files"> <mkdir dir="${build.dir}"/> <javac destdir="${build.dir}" target="1.3" debug="true" deprecation="false" optimize="false" failonerror="true"> <src path="${src.dir}"/> <classpath refid="master-classpath"/> </javac> </target> <target name="deploy" depends="build" description="Deploy application"> <copy todir="${deploy.path}/${name}" preservelastmodified="true"> <fileset dir="${web.dir}"> <include name="**/*.*"/> </fileset> </copy> </target> </project>

 
build.xml放在struts2app目录下,再在这个目录下建一个build.properties文件,内容如下:

按照build.properties配置你的tomcat位置。在struts2app目录下运行ant build ,ant deploy ,程序就发布到tomcat的webapps下

# Ant properties for building the springapp
appserver.home=d:/tomcat5.5
deploy.path=${appserver.home}/webapps

tomcat.manager.url=http://localhost:8080/manager
tomcat.manager.username=admin
tomcat.manager.password=admin
 

运行
现在,启动tomcat,访问
http://localhost:8080/tutorial/HelloWorld.action,能看到页面的title为"Hello World!" ,页面上显示"Struts is up and running!".

它们怎么运行的
1、struts2容器收到HelloWorld.action请求,从web.xml获取设置,org.apache.struts2.dispatcher.FilterDispatcher是所有应用(包括*.action)的入口点。
2、 struts2在struts.xml中找到HelloWorld类(Action),并调用它的execute方法。
3、 execute方法给message变量赋值,并返回SUCCESS,struts2收到SUCCESS标志,按照映射关系,把HelloWorld.jsp返回客户端。
4、  当HelloWorld.jsp开始运行,<s:property value="message" />会调用HelloWorld类getMessage方法,把结果显示在页面上。

分享到:
评论

相关推荐

    学Struts2从HelloWorld示例开始

    学Struts2从HelloWorld示例开始

    struts-helloworld.zip

    Struts2 入门 — HelloWorld 示例源代码

    Struts2 HelloWorld示例(Maven项目)

    Struts2做的一个HelloWorld示例,使用Maven构建,导入请前先安装maven。

    struts2 HelloWorld最小必备包

    这个是struts2入门示例HelloWorld所需的最小必备包。

    struts2 HelloWorld

    struts2 入门示例 HellWorld 工程源码。

    helloworld.war

    Manning.Struts.2.in.Action 's example HelloWorld.war provides a convenient example of a minimal Struts 2 application

    struts2 helloworld example

    自己编写的一个helloworld. 使用struts2.0.11.2, 在eclipse3.4, tomcat5.5, jdk1.5平台上写的代码. 没有使用jarkatar提供的空白模板struts2-blank-2.0.11.2.war(发现这个模板有问题). 已集成到war文件中, 可以使用...

    helloWorld-struts2.0.14.rar

    最简单的源码,一看就明白的配置,struts2初学者入门

    Struts2 in action中文版

    第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...

    webWork2.26 的helloWorld 例子

    STRUTS2学习前 自已做的只有WEBWORK的HELLOWORLD例子 希望对你们有帮助.

    Struts Web设计与开发大全

    18章:Hello World类和测试类以及Struts测试工程; 19章:Struts与Hibernate结合应用; 20章:lucene1全文检索应用,直接复制到tomcat的webapps目录下即可使用; 21-22章:DigitStore应用,数据库脚本等。

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...

    8种Java Web框架安装手记及HelloWorld

    8种Java Web框架安装手记,分别是Grails,GWT,JSF,Play,Spring,Struts,Vaadin,Wacket。全部是截止2013-12-19最新的框架版本,文档最后有链接可以下载所有相关资料和jar包以及示例程序,任何问题可以联系...

    《程序天下:J2EE整合详解与典型案例》光盘源码

    10.3 利用Spring在JSP页面输出“HelloWorld”的示例 10.3.1 建立myHelloWorld工程 10.3.2 编写JSP页面helloWorld.jsp 10.3.3 编写控制器HelloWorldController.java 10.3.4 建立Spring的配置文档dispatcherServlet-...

    Spring-Reference_zh_CN(Spring中文参考手册)

    使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.2.1. PropertyPlaceholderConfigurer示例 3.7.2.2. ...

    FlemingGEOM99

    创建“概述”(Hello World)示例。 位置设置为AB Drumheller 在查看位置 方向API(JSON返回) 遵循指示说明并形成URL以提供JSON中的路线指示 从皇家泰瑞尔博物馆到胡德步道的路线 获取路线: Places API(J

    ActionScript 3.0 API文档及Flex开发详解电子书

    从最简单的Hello World演示到完整的项目全案开发,给读者全新的学习过程。 内容分为四部分:开启Flex之门、Flex开发进阶、Flex 3 Web项目全案开发、Flex 3 AIR桌面项目全案开发。涉及了Flex基础概念、组件设计开发...

Global site tag (gtag.js) - Google Analytics