Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 462|回复: 0

[struts学习]Struts 2 学习笔记:HelloWorld

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 06:15:17 | 显示全部楼层 |阅读模式
    到写本文为止,Struts的最新版本是 2.0.6。Struts 2.0.6的下载地址是:
        http://struts.apache.org/download.cgi#struts206
    为了方便,可以选择下载Full发布包:

      http://mirrors.sirium.net/pub/apache/struts/binaries/struts-2.0.6-all.zip

          struts自带有几个有用的范例,我们可以拿来学习研究一下。本文就是参考子apps目录中的blank项目。      本HelloWorld的需求很简单,只是可以通过英语和汉语两种语言来显示HelloWorld。当然,这不是自动翻译。只是在不同的环境中,读取不同的字符串。

           这是Struts2的入门的第一个范例。是在eclipse 3.2.2 + myeclipse 5.2.2环境下开发的。


       
      
      
          读者可能会问,myeclipse不是自带有struts吗?是的。Myeclipse为了方便开发员,带有struts 1.1和1.2。但是没有Struts 2的开发包。Myeclipse是个很有用的东西,Myeclipse对开发和部署web站点,编辑xml文件,jsp标记很有帮助,因此,我在这提到myeclipse。这不是为myeclipse做广告。当然,你也可以使用eclipse中其他web开发插件。  开发HelloWorld项目的基本步骤是:  1. 建立web项目  2. 导入Struts 2库文件  3. 修改web.xml文件  4. 建立index.HTML文件。  5. HelloWorld.jsp文件  6. Action文件  7. 建立Struts配置文件  8. properties配置文件  1. 建立web项目 新建的的web项目名称是:jeff-struts2-helloworld。    图表 1  Myeclipse产生一些目录结构。下图是在该项目完成时的目录结构。    图表 2  2. 导入Struts 2库文件   图表 3  这5个文件是必须的。在struts网站下载下来文件包里面。把这5个文件导入到WEB-INF/lib目录里。这5个文件自动的加入到构建路径中了。  3. 修改web.xml文件
       
         
         1
        <?
        xml version="1.0" encoding="UTF-8"
        ?>
       

         2
       
        <
        web-app
        version
        ="2.4"
         xmlns
        ="http://java.sun.com/xml/ns/j2ee"
       

         3
            xmlns:xsi
        ="http://www.w3.org/2001/XMLSchema-instance"
       

         4
            xsi:schemaLocation
        ="http://java.sun.com/xml/ns/j2ee

         5
            http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        >
       

         6
       

         7
            
        <
        welcome-file-list
        >
       

         8
                
        <
        welcome-file
        >
        index.html
        </
        welcome-file
        >
       

         9
            
        </
        welcome-file-list
        >
       

        10
       

        11
            
        <
        filter
        >
       

        12
                
        <
        filter-name
        >
        Struts 2
        </
        filter-name
        >
       

        13
                
        <
        filter-class
        >
        org.apache.struts2.dispatcher.FilterDispatcher
        </
        filter-class
        >
       

        14
            
        </
        filter
        >
       

        15
       

        16
            
        <
        filter-mapping
        >
       

        17
                
        <
        filter-name
        >
        Struts 2
        </
        filter-name
        >
       

        18
                
        <
        url-pattern
        >
        /*
        </
        url-pattern
        >
       

        19
            
        </
        filter-mapping
        >
       

        20
       
        </
        web-app
        >
       

        21
       
       
    在web.xml文件中,首先建立首页文件(index.html),这样方便访问。然后加入一个过滤器(filter),过滤器类是:org.apache.struts2.dispatcher.FilterDispatcher 。Struts 2是通过FilterDispatcher这个过滤器(filter)来实现的。  4. 建立index.html文件。
          
      
       
         
         1
        <!
        DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
        >
       

         2
       
        <
        html
        xmlns
        ="http://www.w3.org/1999/xhtml"
        >
       

         3
          
        <
        head
        >
       

         4
            
        <
        title
        >
        index.html
        </
        title
        >
       

         5
            
        <
        meta
        http-equiv
        ="refresh"
         content
        ="0;example/HelloWorld.action"
         
        />
            

         6
          
        </
        head
        >
       

         7
          

         8
          
        <
        body
        >
       

         9
            
        <
        p
        >
        Loading
        </
        p
        >
       

        10
          
        </
        body
        >
       

        11
       
        </
        html
        >
       

        12
       
       
    index.html文件只是用于导向作用。这里有个规则:"Link actions not pages."。意思是说:通过action来访问web,而不是jsp文件。所以,我在index.html导向dao一个HelloWorld.action,而不是HelloWorld.jsp。  5. HelloWorld.jsp文件
       
         
         1
        <%
        @ page language
        =
        "
        java
        "
         contentType
        =
        "
        text/html; charset=utf-8
        "
         pageEncoding
        =
        "
        UTF-8
        "
        %>
       

         2
       
        <%
        @ taglib prefix
        =
        "
        s
        "
         uri
        =
        "
        /struts-tags
        "
        %>
       

         3
       

         4
       
        <?
        xml version="1.0" encoding="utf-8"
        ?>
       

         5
       
        <!
        DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
        >
       

         6
       
        <
        html
        xmlns
        ="http://www.w3.org/1999/xhtml"
        >
       

         7
            
        <
        head
        >
       

         8
                
        <
        meta
        http-equiv
        ="Content-Type"
         content
        ="text/html; charset=utf-8"
         
        />
       

         9
                
        <
        title
        ><
        s:text
        name
        ="helloworld.message"
        ></
        s:text
        >
       

        10
                
        </
        title
        >
       

        11
            
        </
        head
        >
       

        12
            
        <
        body
        >
       

        13
                
        <
        h2
        >
       

        14
                   
        <
        s:property
        value
        ="message"
         
        />
       

        15
                
        </
        h2
        >
       

        16
                
        <
        ul
        >
       

        17
                   
        <
        li
        >
       

        18
                        
        <
        s:url
        id
        ="url"
         action
        ="HelloWorld"
        >
       

        19
                            
        <
        s:param
        name
        ="request_locale"
        >
        en
        </
        s:param
        >
       

        20
                        
        </
        s:url
        >
       

        21
                        
        <
        s:a
        href
        =""
        >
        English
        </
        s:a
        >
       

        22
                   
        </
        li
        >
       

        23
                   
        <
        li
        >
       

        24
                        
        <
        s:url
        id
        ="url"
         action
        ="HelloWorld"
        >
       

        25
                            
        <
        s:param
        name
        ="request_locale"
        >
        zh
        </
        s:param
        >
       

        26
                        
        </
        s:url
        >
       

        27
                        
        <
        s:a
        href
        =""
        >
        Chinese
        </
        s:a
        >
       

        28
                   
        </
        li
        >
       

        29
                
        </
        ul
        >
       

        30
            
        </
        body
        >
       

        31
       
        </
        html
        >
       

        32
       
       
    在实际的项目中,页面的内容可能会比较复杂,可以用dreaweaver来先用静态的文本来填充布局,然后,再用struts 标记来代替这些文本。新建的html和jsp文件,尽可能的遵循xhtml的标准。  在文件的开头,为了使用struts 2标记,必须有这样的一行:  
      
       
         
        <%
        @ taglib prefix
        =
        "
        s
        "
         uri
        =
        "
        /struts-tags
        "
        %>
          在struts 2 中使用文本有两种方法:本项目都采用了,这也是为了演示Struts 2的功能。  
       
        从properties配置文件中读取。如:<s:text name="helloworld.message" /> 就会从properties中读取配置文件中helloworld.message这个配置。  
       
       
        从bean的属性中读取。如:<s:property value="message" />,这就会从本页面相关的上下文中查找到message这个属性并显示出来。  
        <s:url />和<s:a />标记,在这里不打算详细介绍。  6. Action文件 在本项目中,ExampleSupport类继承自ActionSupport类,HelloWorld又继承自ExampleSupport类。在这里ExampleSupport类没有做任何事情。但在实际的项目中,可以把项目中其他Action所需的公共的内容,写在ExampleSupport类中。我这里是尽可能的按项目的方式开发,而完全是按照玩具的方式开发。  ExampleSupport.java文件   
       
         
        1
        package
         jeff.struts2.helloworld;

        2
       

        3
       
        import
         com.opensymphony.xwork2.ActionSupport;

        4
       

        5
       
        public
         
        class
         ExampleSupport
        extends
         ActionSupport {

        6
        }

        7
       
       
       HelloWorld.java文件   
         
          
          1
         package
          jeff.struts2.helloworld;

          2
         

          3
         
         public
          
         class
          HelloWorld
         extends
          ExampleSupport {

          4
             

          5
             
         private
          String message;

          6
         

          7
             
         public
          String getMessage() {

          8
                
         return
          message;

          9
             }

         10
         

         11
             
         public
          
         void
          setMessage(String message) {

         12
                
         this
         .message
         =
          message;

         13
             }

         14
         

         15
             @Override

         16
             
         public
          String execute()
         throws
          Exception {   

         17
                 setMessage(getText(
         "
         helloworld.message
         "
         ));

         18
                
         return
          SUCCESS;

         19
             }

         20
         }

         21
         
         
    7. 建立Struts配置文件 在struts2-core-2.0.6.jar文件中,有struts-2.0.dtd文件。Struts就是通过该文件来验证struts配置文件的。在本项目中,有两个配置文件:struts.xml文件和example.xml文件。  struts.xml文件:   
       
         
         1
        <?
        xml version="1.0" encoding="UTF-8"
        ?>
       

         2
       
        <!
        DOCTYPE struts PUBLIC

         3
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

         4
            "http://struts.apache.org/dtds/struts-2.0.dtd"
        >
       

         5
       

         6
       
        <
        struts
        >
       

         7
            
        <
        constant
        name
        ="struts.enable.DynamicMethodInvocation"
         value
        ="false"
        ></
        constant
        >
       

         8
            
        <
        constant
        name
        ="struts.devMode"
         value
        ="true"
        ></
        constant
        >
       

         9
            

        10
            
        <
        include
        file
        ="example.xml"
        ></
        include
        >
       

        11
       

        12
       
        </
        struts
        >
       

        13
       
       
       example.xml文件   
       
         
         1
        <?
        xml version="1.0" encoding="UTF-8"
        ?>
       

         2
       
        <!
        DOCTYPE struts PUBLIC

         3
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

         4
            "http://struts.apache.org/dtds/struts-2.0.dtd"
        >
       

         5
       

         6
       
        <
        struts
        >
       

         7
            
        <
        package
        name
        ="example"
         namespace
        ="/example"
         extends
        ="struts-default"
        >
       

         8
                
        <
        action
        name
        ="HelloWorld"
         class
        ="jeff.struts2.helloworld.HelloWorld"
        >
       

         9
                   
        <
        result
        >
        /example/HelloWorld.jsp
        </
        result
        >
       

        10
                
        </
        action
        >
       

        11
            
        </
        package
        >
       

        12
       
        </
        struts
        >
       

        13
       
       
        在这里,我再说一句:本项目尽可能的按项目的方式开发,而完全是按照玩具的方式开发。因为在实际项目中,Struts要配置的内容可能有很多,为了方便编辑和维护struts配置文件,把struts.xml文件拆分成多个文件,然后在struts.xml文件中用<include file />标记把这些配置文件加载进来。  8. properties配置文件 我喜欢在页面每加入一个需要该配置文件中的配置时,就往package.properties中加入一项。在适当的时候,再来把package.properties文件翻译成不同语言的配置文件。  package.properties

      
       
         
        helloworld.message
        =
        Struts is up and running
        package_en.properties  
       
         
        helloworld.message
        =
        Struts is up and running
       
    package_zh.properties  
       
         
        helloworld.message
        =
        Strutsu5df2u7ecfu642du5efau597du4e86uff0cu5e76u5207u5728u8fd0u884cu2026u2026
       
    不能在中文的pakcage_zh.properties配置文件中,直接写汉字。在eclipse中有插件,可以直接转换。Sun公司的jdk自带有个工具转换,叫做native2ascii。我在eclipse中配置了一个外部工具,工具配置如下:    图表 4  native2ascii的命令格式是这样的:native2ascii sourcefile desitationfile。这个工具配置用到了一个技巧。就是在package_zh文件(注意:该文件没有扩展名)中写汉字,然后运行该工具,就生成了package_zh.properites文件。每次改动package_zh文件,并要部署项目前,先运行一下该工具。  package_zh文件的内容如下:   
       
         
        helloworld.message
        =
        Struts已经搭建好了,并切在运行 ……
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 07:20 , Processed in 0.365915 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表