`
yunhaifeiwu
  • 浏览: 162364 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

工作流程与hello World!

阅读更多

struts2 工作流程与HelloWorld


注:使用netbeans6.5调试

.页面请求(jsp)——>逻辑控制器(Filter)——>拦截器(Interceotor)——>业务控制器(Action)——>返回处理器(Result) ——>返回视图

1 浏览器中输入网址发出请求
2  web服务器(servlet)通过web.xl的配置,找到sturts过滤器,把以”.acton”结尾的请求,交给 struts过滤器处理。
3  struts过滤器寻找struts.xml配置文件——>调用struts拦截器——>调用sturts的action——>调用sturts的result——>返回视图。

     其中:1 网页与stuts2的数据交流是根根action中的定义的getter 、setter方法进行。这简化了使用。
      2 servlet过滤器: servlet对某种规格的网址进行过滤,并交给符号专门接口的类进行处理。
      3 网页与代码的交互:
        当action类的变量有getter and setter方法时
代码方面:如果网页(html,jsp等)中的UI标签的name属性值就是变量名时,action类直接访问该变量就能得到网页中UI标签的数据。
网页方面:
(1) 如果是jsp,且具有struts2标签,并且网页中的UI标签的name属性值就是变量名时, 网页中的UI标签 直接就能得到该变量的数据。对于非UI的数据标签,有的是通过value值为变量名的方式,得到该变量的数据,这种标签有property
(2) 如果是纯jsp。则网页通过<%=request.getAttribute("变量名") %> 的方式,得到该变量的数据。
(3) 支持FreeMaker或veloctity的网页,如果该变量是一个类实例,则可通过${变量名.属性名}的方式得到该变量的属性值,如果不是一个实例,则可通过${变量名}的方式得到该变量的数据。 
(4) 其中struts2中的jsp, 如果该变量是一个类实例,则可通过${变量名.属性名}的方式得到该变量的属性值,如果不是一个实例,则可通过${变量名}的方式得到该变量的数据.


       例子见:网页与代码交互

用helloWorld中的例子说明:
1 输入网址 http://localhost:8084/Web2/helloxx.action
2  web服务器,找到FilterDispatcher过滤器,把该请求交给它。
3  FilterDispatcher根据struts配置,调用Hello.java程序(由于没有拉截器,也没有result,所以仅调用 这个action),执行其中的execute方法。根据约定,hello.java中的name,message变量将得到 发出网址中的网页(如果是由网页发出的请求,例中点hello.jsp中的“submit”按钮,将发出helloxx.action请求)得到值。
4 执行完毕,返回success字符串,FilterDispatcher要据这个字符串与struts.xml配置,调用hello.jsp页面。
5 根据约定,hello.jsp将调用名为Hello的action,取得name,message属性。
6 浏览器用户,将看到hello网页。

HelloWorld下例具体使用Struts2。
第一步 配置WEB.XML--------Struts2过滤器
打开工程中的web.xml
输入:

<filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <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文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <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>

    
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 </web-app>


第二步 建action
自建的action将继承struts2的ActionSupport(实现了检验功能)
覆盖execute方法
Hello.java
package hi;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Hello extends ActionSupport
{
    public String message="";
    public String name="";
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    	
    
    @Override
    public String execute(){
        message = "hello "+name+"!\n";
        return "success";
    }	
	
}


第三步建struts.xml
该配置位于存位类的文件夹下(第0层)netbeans---为src/java
下面蓝色为固定的。红色的,extends 为“json-default”时,表示以json的形式组建数据,以用于ajax模式。这里是struts方式
<?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>
    <package name="hi" namespace="/"   extends="struts-default">
        <action name="helloxx" class="hi.Hello">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>




第四步,建hello.jsp
存于专放网页的文件夹下 (第0层)  netbeans---为web
蓝色为使用了struts2标签的固定格式。
红色与action中的属性(有get、set方法)相对应。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
   <!-- 测试action中的message属性是否有值 -->
   <s:if test="message != null">
      <font color="red">
        <!-- 得到message的值,并显示在网页中 -->
        <s:property value="message"/>
       </font>
     </s:if>
     Please enter your name:
    <!-- 这是一个 form,将投交的是hello.action网址 -->
    <s:form action = "helloxx.action">
      <!-- 这是一个文本标签,它与名为Hello的Action的name属性相关联 -->
      < s:textfield name ="name"/>
      <s:submit/>
    </s:form> 
  </body>
</html>

最后运行
本软件是在netbeans6.5中测试。工程为web2
调试运行:
http://localhost:8084/Web2/helloxx.action

注意事项:
1 action 与strut..xml中的配置要对应。Action类中返回的每种字符串,要在配配置中有节点result相对应 如:Hello.java中返回有"success"字符串,则在struts.xml中,有<result name="success">/hello.jsp</result>相对应

2 struts.xml 的namespace节点配置,意味着其下的返回网页必须与之相对应存放。Result节点的配置也要相对应。如:namespace=”/”,则Result的配置如
<result name="success">/hello.jsp</result>,同时hello.jsp必须存放于网页的根目录中。在netbeans中,hello.jsp必须在web文件夹下。
3 一个action的网址是:
  网址根目录+ action名字。
如:Hello.java 的配置为:<action name="helloxx" class="hi.Hello">
而工程又是web2. 则在netbeans中网址是:
http://localhost:8084/Web2/helloxx.action
注意:蓝色色字体
4 网址中的action名字要区分大小写
5 网页hello.jsp 与Hello.java的数据交互是通过Hello.java中有get、set的属性进行交互。
struts2中的lib 为
引用
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar

如果:struts2-spring-plugin 包存在将无法起动 apache tomcat服务器


7struts.xml中可引入别的action配置。便于排错或维护
本例中,可如下用两个xml文件写配置。这两个文件为:Struts.xml , struts-hello.xml。这两个文件的头都一样。下例中两个配置文件同一个文件夹下
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>
    <include file="struts-hello.xml" />
</struts>

struts-hello.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>
    <package name="hi" namespace="/"   extends="struts-default">
        <action name="helloxx" class="hi.Hello">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    c++显示hello world!

    - 当程序运行时,控制台将输出 “Hello world!” 字符串。 - 注意到这里的字符串没有结尾的感叹号,因为宏定义中的字符串是 `Hello world!`,而在程序中实际上是 `Hello world`。 #### 三、扩展知识点 1. **C++...

    Hello,World!

    这个简单的程序在屏幕上打印出 "Hello, World!" 文本,帮助开发者了解如何在特定的语言环境中控制输出。下面我们将详细讲解不同编程语言中的 "Hello, World!" 程序示例。 1. **RPGLE (AS400)** RPGLE(Report ...

    关于"Hello World!"程序

    std::cout &lt;&lt; "Hello, world!" ; std::operator(std::cout, "Hello, world!"); // friend cout是模板类的实例 std::cout.operator(std::endl); // member endl是函数模板指针 std::operator(std::...

    Hello world!

    在大多数编程语言中,这个程序非常简单,通常只包含一行代码,用于在控制台打印出 "Hello, World!" 这个字符串。 在描述中提到的博客链接指向了 iteye.com 上的一个博客条目,作者 Jimmy Lam 分享了他的见解或教程...

    变幻七彩HELLO WORLD!

    在8086汇编语言编程环境中,"变幻七彩HELLO WORLD!"是一个独特的程序示例,它展示了如何在单色屏幕上实现颜色变换的效果。这个程序的核心在于使用特定的指令序列来改变屏幕上的文本颜色,从而达到“变幻七彩”的...

    第一个Python程序,hello world!

    第一个Python程序,hello world!

    hello world!

    这个简单的程序通常会打印出 "Hello World!" 这个字符串,向初学者展示如何在控制台上输出文本。 学习程序是一个持续的过程,涵盖广泛的主题,包括但不限于以下几点: 1. **基础语法**:理解编程语言的基本结构,...

    简单的消息框源程序hello world!(汇编源码)

    在这个案例中,"简单的消息框源程序hello world!".asm 文件就是源码,而".exe"文件是经过MASM汇编和链接后的可执行程序。 消息框通常由操作系统提供服务,比如在Windows环境下,可以通过调用API函数来创建。在汇编...

    QT5.14入门教程GUI(一)第一个QT程序Hello World! -HelloWorld.7z

    QT5.14入门教程GUI(一)第一个QT程序Hello World! QT5.14入门教程GUI(一)第一个QT程序Hello World! QT5.14入门教程GUI(一)第一个QT程序Hello World!

    Helloworld_helloworld_

    标签 "helloworld" 明确指出了这个主题与 "Hello, World!" 程序有关。至于压缩包中的 "Helloworld.txt" 文件,很可能包含了上述编程示例的源代码,或者是关于这个主题的额外说明或练习。 总的来说,"Hello, World!...

    hellopython的代码-python基础教程之HelloWorld!.pdf

    然后,回到命令行,输入`$python hello.py`来运行这个程序,同样会打印出"Hello World!"。 此外,为了使Python脚本可以直接作为可执行文件运行,你需要在文件顶部添加一行特殊注释,告诉操作系统如何找到Python解释...

    Objective-C HelloWorld! 程序案例

    ### Objective-C HelloWorld! 程序案例知识点解析 #### 一、Objective-C语言概述 Objective-C是一种面向对象的编程语言,由苹果公司在其操作系统(如macOS、iOS、watchOS和tvOS)的开发中广泛使用。Objective-C...

    例程1-Hello World!.zip

    5. **串口通信**:如果程序通过串口监视器显示“Hello World!”,则会使用`Serial.begin()`设置波特率,`Serial.println()`发送数据。 6. **库函数**:Arduino库提供了许多预定义的函数,例如`delay()`用于设置延时...

    例程1-Hello World!.zip_Arduino基础程序_world

    在这个例子中,我们可能将看到如何编写一个简单的程序,当Arduino接收到特定的指令时,在串行监视器上显示 "Hello, World!"。这通常涉及到串行通信、条件语句和字符串处理。 首先,我们需要了解Arduino的编程环境...

    helloworld!

    "HelloWorld!" 是一个经典的编程入门程序,通常用于教授初学者如何在各种编程语言中打印出 "Hello, World!" 这一字符串。这个简单的程序帮助新手理解基本的代码结构、语法以及程序运行的基本流程。 在编程领域,...

    Hello World ! Java ! 记录Java学习日常.zip

    然后通过`java HelloWorld`运行程序,屏幕上将显示“Hello, World!”。 【Java语法特性】 1. **面向对象**:Java支持类、对象、封装、继承和多态等面向对象特性。 2. **自动内存管理**:Java使用垃圾收集器自动...

    零基础学C/C++01——第一个程序Hello World!

    零基础学C/C++01——第一个程序Hello World! 答案很简单 只要学过c++ #include using namespace std; int main() { cout &lt;&lt;"Hello World!" ; return 0; }

    面向对象C++示例 hello world!

    【标题】:“面向对象C++示例 hello world!”揭示了C++编程中的核心概念——面向对象编程(Object-Oriented Programming, OOP),并结合了基础的用户界面交互,通过一个简单的“hello world”程序来展示。在C++中,...

    Hello_World.zip_hello world _world

    这个简单的程序是许多编程语言教学的第一课,用于向初学者介绍如何在代码中打印出“Hello, World!”这个字符串,以此来验证编译环境的正确配置和基础语法的理解。 在IT领域,“Hello, World!”程序的重要性不言而喻...

    Objective-C 2.0 with Cocoa Foundation --- 2,从Hello,World!开始

    本教程将深入探讨Objective-C 2.0与Cocoa Foundation的结合,从经典的"Hello, World!"程序开始,逐步揭示这门语言的强大之处。 "Hello, World!"是所有编程旅程的传统起点,它展示了语言的基本语法和输出功能。在...

Global site tag (gtag.js) - Google Analytics