zoukankan      html  css  js  c++  java
  • Struts2(一)基本配置

    一、Struts2概述

      1、什么是Struts2?

      Struts2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样使得业务逻辑控制器能够和ServletAPI脱离开来。

      2、工作原理

      当web容器接收到HttpServletRequest请求后,容器通过web.xml映射请求,并获得控制器的名字,然后容器调用控制器(StrutsPrepareAndExecuteFilter),控制器通过ActionMapper获取Action的信息并调用ActionProxy,ActionProxy读取struts.xml文件获取action和拦截器栈的信息,ActionProxy把请求传递给ActionInvocation,由ActionInvocation依次调用action和interceptor,根据action的配置信息,产生result,result信息返回给ActionInvocation并产生一个HttpServletResponse响应,将响应发送给客户端。

      上述的步骤可由下图表示:

      

    二、配置环境的基本步骤

      1、在Eclipse中新建一个动态web工程。首先配置其web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>day_0818</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
       <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>
    </web-app>

      2、向工程中导入满足struts2开发的基本jar包

      

      3、在src目录下配置struts.xml文件,其中的字段有如下:

      package:包,struts2使用package来组织模块

         name属性:必须,用于其它的用应用当前包

      extends:当前继承哪个包,继承的,既可以继承其中的所有的配置,通常情况下继承struts-default, struts-default这个包在struts-default.xml文件中定义

      namespace:可选,如果没有给出,默认为“/”,若namespace有一个非默认值,则要想调用这个包里的Action,就必须把这个属性所定义的命名空间添加到有关的URI字符串里

    <?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>
        <constant name="struts.devMode" value="true" />
        <package name="default" namespace="/" extends="struts-default">
            <action name="index">
                <result>/index.jsp</result>
            </action>
        </package>
        <include file="example.xml"/>
        <!-- Add packages here -->
    </struts>

      4、在服务器上运行该工程,即可显示index.jsp页面内容

      

    三、注意事项

      1、具体视图的返回可以由用户自己定义的Action来决定,具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容,具体的Action实现可以是一个普通的Java类,里面有public String execute方法即可或者实现Action接口,最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法。另外,Action执行的时候并一定是要执行execute方法,可以在配置文件中配置Action的时候用method=来指定执行哪个方法,也可以在url地址中动态指定(DMI,推荐使用)。

      

  • 相关阅读:
    spring-boot配置文件中server.context-path不起作用的解决方案
    SpringBoot常见错误页面,Whitelabel Error Page解决办法(type=Not Found, status=404)
    JPA通用策略生成器(@GeneratedValue 四种标准用法为TABLE, SEQUENCE, IDENTITY, AUTO)
    springBoot+jpa 测试自增时数据库报错Springboot-jpa Table 'sell.hibernate_sequence' doesn't exist
    Java 枚举(enum) 详解7种常见的用法
    IntelliJ IDEA使用教程 (总目录篇)非常详细,适合各个层次的人群学习
    数据库之MySQL数据库视图:视图定义、创建视图、修改视图
    Java日志管理:Logger.getLogger()和LogFactory.getLog()的区别(详解Log4j)
    谷歌浏览器Chrome开发者工具详解
    基本数据类型----字符串
  • 原文地址:https://www.cnblogs.com/xujian2014/p/4739985.html
Copyright © 2011-2022 走看看