zoukankan      html  css  js  c++  java
  • Struts2简介以及结果集转发

    一、分析之前的项目的不足,编写属于自己的框架
    二、Struts2简介(面试)
    三、搭建Struts2的开发环境
    1、找到所需的jar包:发行包的lib目录中(不同版本需要的最小jar包是不同的,参见不同版本的文档。2.1.7)
    去apps中找例子拷贝jar包

    2、在应用的WEB-INF/classes目录下建立一个名称为struts.xml的配置文件,内容如下:

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE struts PUBLIC
    3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    4 "http://struts.apache.org/dtds/struts-2.3.dtd">
    5 <struts>
    6 
    7 </struts>

    3、配置核心控制器,就是一个过滤器

    1 <filter>
    2 <filter-name>struts2</filter-name>
    3 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    4 </filter>
    5 <filter-mapping>
    6 <filter-name>struts2</filter-name>
    7 <url-pattern>/*</url-pattern>
    8 </filter-mapping>


    4、如果TOmcat启动成功,没有报错,证明环境搭建成功!

    四、开发第一个Struts2案例
    1、编写struts.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
     4 "http://struts.apache.org/dtds/struts-2.1.7.dtd">
     5 <struts><!--这是Struts2配置文件的根元素-->
     6 <package name="itcast" namespace="/test" extends="struts-default">
     7 <!--
     8 pageckage:方便管理动作元素
     9 name:必须有。包的名称,配置文件中必须保证唯一。
    10 namespace:该包的名称空间,一般是以"/"开头
    11 extends:集成的父包的名称。struts-default名称的包是struts2框架已经命名好的一个包。(在struts2-core.jar中有一个struts-default.xml中)
    12 abstract:是否是抽象包。没有任何action元素的包就是抽象包(java类)
    13 
    14 -->
    15 <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="sayHello">
    16 <!--
    17 action:代表一个请求动作
    18 name:同包中必须唯一。动作的名称
    19 class:负责处理的JavaBean的类全名
    20 method:JavaBean中的对应处理方法。(动作方法:特点是,public String 方法名(){})
    21 -->
    22 <result name="success">/1.jsp</result>
    23 <!--
    24 result:结果类型
    25 name:动作方法返回的字符串
    26 主体内容:View的具体地址。
    27 -->
    28 </action>
    29 </package>
    30 </struts>


    2、根据配置文件,创建需要的javabean和对应的动作方法, 在动作方法中完成你的逻辑调用。

     1 package cn.itcast.action;
     2 
     3 public class HelloWorldAction implements Serializable {
     4 private String message;
     5 
     6 public String getMessage() {
     7 return message;
     8 }
     9 
    10 public void setMessage(String message) {
    11 this.message = message;
    12 }
    13 public String sayHello(){
    14 message = "helloworld by struts2";
    15 return "success";
    16 }
    17 }

    3、编写View,显示结果
    ${message}

    4、访问helloworld动作的方式:http://localhost:8080/struts2day01/test/helloworld 应用名称/包的名称空间/动作的名称
    默认情况下:访问动作名helloworld,可以直接helloworld,或者helloworld.action

    http://localhost:8080/struts2day01/test/a/b/c/helloworld
    /test/a/b/c:名称空间
    helloworld:动作名称

    搜索顺序:名称空间
    /test/a/b/c 没有helloworld
    /test/a/b 没有helloworld
    /test/a 没有helloworld
    /test 有了,调用执行

    五、Struts2配置文件的详解
    1、struts.xml配置文件编写是没有提示的问题?
    方法一:上网即可
    方法二:
    1、拷贝http://struts.apache.org/dtds/struts-2.1.7.dtd地址
    2、Eclipse的window、preferences,搜索XML Catelog
    3、点击add按钮
    Location:dtd文件的路径
    Key Type:URI
    Key:http://struts.apache.org/dtds/struts-2.1.7.dtd
    2、Struts配置文件中的各种默认值。
    action:
    class:默认值是com.opensymphony.xwork2.ActionSupport
    常量: SUCCESS success
    NONE none
    ERROR error
    INPUT input
    LOGIN login
    method:默认值是public String execute(){}

    实际开发中:自己编写的动作类一般情况下继承com.opensymphony.xwork2.ActionSupport
    result:
    type:转到目的地的方式。默认值是转发,名称是dispatcher
    (注:type的取值是定义好的,不是瞎写的。在struts-default.xml中的package中有定义)

     1 <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
     2 <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
     3 <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
     4 <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
     5 <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
     6 <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
     7 <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
     8 <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
     9 <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
    10 <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />


    dispatcher:普通的转发到某个页面
    chain:普通的抓发到某个动作名称
    redirect:重定向到一个页面
    redirectAction:重定向到一个动作名称
    plainText:以纯文本的形式输出JSP内容
    result元素的写法:
    方式一:

    1 <result type="chain" name="success">a2</result>


    方式二:

    1 <result type="chain" name="success">
    2 <param name="actionName">a2</param><!--name对应的chain的处理器中的setActionName()方法-->
    3 </result>


    注意:如果要转向的是在另外一个名称空间的动作,那么只能使用方式二

     1 <package name="p1" namespace="/namespace1" extends="struts-default">
     2 <action name="a2">
     3 <result type="dispatcher" name="success">/3.jsp</result>
     4 </action>
     5 </package>
     6 <package name="p2" namespace="/namespace2" extends="struts-default">
     7 <action name="a1">
     8 <result type="chain" name="success">
     9 <param name="namespace">/namespace1</param>
    10 <param name="actionName">a2</param>
    11 </result>
    12 </action>
    13 </package>


    3、开发中配置文件的更改,在访问时让框架自动重新加载:
    struts.devMode = false(default.properties)
    利用strutx.xml中的constant元素来覆盖掉default.properties默认行为

    1 <struts>
    2 <constant name="struts.devMode" value="true"></constant>
    3 </struts>
    4 
    5  

    在struts.xml中写标签的时候,有些转发器 或者参数  可以查看 struts2-core-2.3.16.3.jar包中的org.apache.struts2中的default.properties默认配置文件代表strus2启动的一些参数,可以修改的。  还有struts-default.xml代表strus.xml中参照的一些类型 和类的 配置

  • 相关阅读:
    node-webkit 不支持html5_video播放mp4的解决方法
    node-webkit(Windows系统) 打包成exe文件后,被360杀毒软件误报木马的解决方法
    剑指 Offer 36. 二叉搜索树与双向链表
    剑指 Offer 33. 二叉搜索树的后序遍历序列
    Leetcode96. 不同的二叉搜索树
    Leetcode95. 不同的二叉搜索树 II
    leetcode1493. 删掉一个元素以后全为 1 的最长子数组
    Leetcode1052. 爱生气的书店老板
    Leetcode92. 反转链表 II
    Leetcode495. 提莫攻击
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3765310.html
Copyright © 2011-2022 走看看