web学习笔记23-Struts2简介,配置

1、Struts2的简介:

Struts2是基于MVC开发模型的一个框架,属于表现层框架。
我们知道web应用分三层架构(表现层,服务层,数据访问层dao)
每一层都有个对应的框架
表现层:Struts2
服务层:Spring
数据访问层:hibernate,mybatis

注意:三层架构和MVC模式没有任何关系,一个是软件的架构,一个是设计的模式。

sun公司推出的2中设计模式
模型1:JSP+JavaBean
模型2:MVC(Model, View ,Controler)

这里写图片描述

2、为什么要使用Struts2,有什么好处?

Struts2是对MVC模型的实现,许多讲解Struts的书用Servlet做了个符合MVC2要求的Web应用,再用Struts做了个同样功能的Web应用。
但是在对两种方式的对比中,我发现Struts似乎并没有为开发者带来很大的方便。
两者主要区别:
    视图:两者一样
    控制器:利用Struts并不能完全摆脱这一层,开发者还是需要写Action.使用Servlet方式,也是写一个同Action一样的Servlet充当控制器。
        两者在代码量上没有区别,在程序逻辑上也一样;
    模型:两者一样
两者的主要差别:Struts多了一个ActionServlet。
    ActionServlet的作用是拦截用户请求,并将用户请求转发给合适的Action;
    而自己的Web应用是将用户请求直接发送给功能等同于 Action的自定义Servlet

所以Struts2为开发者带来了如下实际的好处:
    a.通过ActionMapping,Action在转发时,并不是转发给一个实际的页面。
        而是转发给在strus-config.xml中已经配置的对象。这意味着,在不改变Action代码的情况下就可以更换其转发的页面.
    【如果没有ActionMapping,当有100个Action都要更换转发页面时,我们不得不在庞大的Web应用中找出这100个Action,修改其转发页面,然后再重新编译它们。
    有了ActionMapping后,只需要在 struts-config.xml中修改相应的配置即可,这样既查找方便,又不用重新编译】。

    b.Struts2可以规范程序员的行为。
        也许Struts2并不能降低实际的代码量,甚至有时候不使用Struts的代码可能更简洁,但是按照Struts2写出来的Web应用却一定是符合MVC模型的。
    【每次开发Web应用,使用Servlet就可以让我实现MVC模型,都必须有意识的严格按照MVC规范来写。
    这看起来不是很困难,但是做起来却很难。有时候在业务处理过程中嵌入几行关于界面的代码似乎是非常自然而且简单的。
    一段时间之后,我突然发现我的业务处理过程和界面显示部分又混杂在一起了】。

3、 Struts2开发环境的搭建:

a.下载Struts2的发行包:http://struts.apache.org .
b.新建一个JavaWeb工程,拷贝一下jar包到lib目录中.
    小技巧:lib里面的jar包太多了,一个个找费事,我们可以从Struts2发行包apps\struts2-blank\WEB-INF\lib,把其中jar都拷贝过来即可。
    一定要拷贝对应的版本jar。

这里写图片描述

c.新建一个src目录,在根目录创建一个配置文件struts.xml
    内容:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
            "http://struts.apache.org/dtds/struts-2.3.dtd">

        <struts>

        </struts>
    注意:dtd版本一定要对。

d.配置控制器映射:框架提供
    内容:
        <?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">
          <display-name></display-name>
          <!-- 配置Struts2框架的核心控制器 -->
          <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
          </filter> 
          <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>

          <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
          </welcome-file-list>
        </web-app>

这里写图片描述

e.部署到Tomcat中,启动,如果没有错,成功。

这里写图片描述

4、简单的小案例:

a.编写index.jsp页面
        <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
        <%
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        %>

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
          <head>
            <base href="<%=basePath%>">

            <title>My JSP 'index.jsp' starting page</title>
            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">    
            <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            <meta http-equiv="description" content="This is my page">
            <!--
            <link rel="stylesheet" type="text/css" href="styles.css">
            -->
          </head>

          <body>
            <a href="${pageContext.request.contextPath}/helloworld.do">第一个案例</a>
          </body>
        </html>
b.修改struts.xml配置文件
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
            "http://struts.apache.org/dtds/struts-2.3.dtd">
        <struts>
            <!-- 开发模式:每次访问时都会重新读取xml配置文件信息 -->
            <constant name="struts.devMode" value="true"></constant>
            <constant name="struts.action.extension" value="do"></constant>
            <package name="p1" extends="struts-default">
                <action name="helloworld" class="com.example.action.HelloWroldAction" method="sayHello">
                    <result name="success">/success.jsp</result>
                    <result name="error">/error.jsp</result>
                </action>
            </package>
        </struts>
c.编写动作类和动作方法
        package com.example.action;
        public class HelloWroldAction {
            public String sayHello(){
                System.out.println("动作方法执行了");
                return "error";//返回error就会调用 error.jsp页面
            }
        }
d.编写success.jsp或error.jsp页面。

在浏览器中访问即可。
注意:可以看到我们开发主要做的就是编写页面和动作类。我们没有再去写servlet了,框架已经封装好了。
但是底层还是使用servlet的知识。

这里写图片描述

5、让编写struts.xml有提示:

我们发现没联网的时候,在编写配置文件的时候没有提示,没有提示就是找不到dtd
http://struts.apache.org/dtds/struts-2.3.dtd
解决方法:
a.联网自动就OK了
b.没有网络情况,手动配置
    在WEB-INF目录下建个dtd文件夹
    在struts2-core.jar中找到dtd文件,拷贝到dtd文件夹
    再到myeclipse中配置好对应的关系就OK了

这里写图片描述

执行的原理:
这里写图片描述

6、Struts2的配置文件:

Struts2框架会按照以下顺序加载配置文件
    a.default.properties:
        在struts2-core**.jar org.apache.struts包中,可以把jar里面的文件拉出来看里面怎么配置的。
    b.struts-default.xml:
        在struts2-core**.jar中。
    c.struts-plugin.xml:
        在插件的jar包中。
    下面3种我们可以自己配置
    d.struts.xml:(推荐使用)
        在应用的构建路径顶端,src目录下。自己定义的Struts配置文件(推荐)
    e.struts.properties:
        在应用的构建路径顶端。(不推荐)
    f.web.xml:配置过滤器时,指定参数。(不推荐)

注意:顺序是固定的。后面的配置会覆盖前面的同名配置信息。所以我们只要在struts.xml中配置了相关的属性就可以了。

例如:Struts2的默认请求路径是***.action , 我们想修改为***.do
    我们在default.properties中看到的配置是
    struts.action.extend=action,,
    表示默认就是action或者空的访问都可以
        http://localhost:8080/Demo/helloworld
        http://localhost:8080/Demo/helloworld.action
    这两种访问都行。

现在我们要修改为.do访问
    a.我们修改struts.xml配置
        <constant name="struts.action.extension" value="do"></constant>

这里写图片描述

    b.在struts.properties这个里面配置
        struts.action.extension=do

这里写图片描述

    c.在web.xml中配置,

这里写图片描述

            <filter>
                <filter-name>struts2</filter-name>
                <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
                    <init-param>
                        <param-name>struts.action.extension</param-name>
                        <param-value>action,,</param-value>
                    </init-param>
            </filter>   
我们只需要使用第一种就行了。

7、struts2配置文件中的元素-package:

相当于java中的包,意义就是分模块开发。
属性:
    name:必须的。配置文件中要唯一。就是一个名字。
    extends:指定父包。会把父包中的配置内容继承下来。
        一般需要直接或间接的继承一个叫做“struts-default”的包(在struts-default.xml配置文件中)。如果不继承该包,那么Struts2中的核心功能将无法使用。
    abstract:是否是抽象包。没有任何action子元素的package可以声明为抽象包。
    namespace:指定名称空间。一般以”/”开头。该包中的动作访问路径:namesapce+动作名称。如果namespace=””,这是默认名称空间,和不写该属性是一样的。

一般配置 name 和 extends就可以

8、struts2配置文件中的元素-action:

作用:定义一个动作。
属性:
    name:必须的。动作名称。用户用于发起请求。在包中要唯一。
    class:指定动作类的全名。框架会通过反射机制实例化。默认是:com.opensymphony.xwork2.ActionSupport。
    method:指定动作类中的动作方法。框架会执行该方法。默认是execute()。

9、struts2配置文件中的元素-result:

name:动作方法返回的字符串值。
<package name="p1" extends="struts-default">
    <action name="helloworld" class="com.example.action.HelloWroldAction" method="sayHello">
        <result name="success">/success.jsp</result>
        <result name="error">/error.jsp</result>
    </action>
</package>

如果HelloWroldAction这个动作类的,sayHello方法返回的是success,就跳转到 success.jsp页面去。

10、动作类:

编写动作类的三种方式:
    a.POJO(Plain Old Java Object)普通的JavaBean。就是不继承任何对象,就一个光秃秃的类加上方法。
        public class HelloWroldAction {
            public String sayHello(){
                System.out.println("动作方法执行了");
                return "success";
            }
        }

    b.实现com.opensymphony.xwork2.Action接口
        Action接口中的常量:
        String SUCCESS:success。一切正常。
        String NONE:none。动作方法执行后,不转向任何的结果视图。或者在动作方法中返回null。
        String ERROR:error。动作方法执行时遇到异常,转向错误提示页面。
        String INPUT:input。验证、转换失败,转向输入页面。
        String LOGIN:login。检测用户是否登录,没有登录转向此视图。

    c.继承com.opensymphony.xwork2.ActionSupport(推荐使用)
        好处:提供了一些基本的功能。比如验证和国际化消息提示等。
        public class HelloWroldAction extends ActionSupport{

        }

动作类中的动作方法:
    编写要求:
    public String XXX(){

    }

11、动作类中获取ServletAPI:

我们使用框架写,我们经常会使用到Servlet中的API,
    public class HelloWroldAction extends ActionSupport{
        public String execute() throws Exception{
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            ServletContext application = ServletActionContext.getServletContext();

            return null;
        }
    }
这样我们就可以拿到了Servlet中的这些对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值