• Struts2 HelloWorld_1


    新建web Project:struts2_0100_introduction

    下载:Struts-2.1.6.zip

    解压后,在apps文件夹下面有5个*.war文件,解压struts2-blank.war文件

      得到:struts.xml文件和lib目录下面的所有*.jar文件

      把得到的struts.xml文件复制到我们项目的src目录下面;

      Build Path的时候把lib目录下面的jar文件添加进我们的项目,虽然说有些jar文件

    对我们现在的项目带来好处,但是我们为了简单起见就一起给添加进来了,这样做也

    也是为了保险。^_^

      修改web.xml文件:

    ------------------------------------Hongten--------------------------------------

    web.xml

    代码;

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

    ------------------------------------Hongten--------------------------------------

    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>
        <package name="default" namespace="/" extends="struts-default">
            <action name="hello">
                <result>
                    /Hello.jsp
                </result>
            </action>
        </package>
    </struts>

    ------------------------------------Hongten--------------------------------------

    Hello.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> Hello Struts2</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>
        Hello Struts2<br>
      </body>
    </html>

    ------------------------------------Hongten--------------------------------------

    部署项目后,启动服务器,输入:http://localhost:1000/struts2_0100_introduction/hello.action

    浏览器输出:

    Hello Struts2

    当然这里可以直接输入:http://localhost:1000/struts2_0100_introduction/hello

    效果也是一样的;

    ------------------------------------Hongten--------------------------------------

    测试二:

    我们要想在修改我们的程序后,不用重新启动服务器等一些麻烦的操作

    如:

    <action name="hello_struts">

      <result>  

        /Hello.jsp

      </result>

    </action>

    修改后我们不用重启服务器,就能让我们的程序运行起来

    这时,我们如果不做任何的配置或部署的话,直接运行会报错的

    如错误信息:

    HTTP Status 404 - There is no Action mapped for namespace / and action name hello_struts.


    type Status report

    message There is no Action mapped for namespace / and action name hello_struts.

    description The requested resource (There is no Action mapped for namespace / and action name hello_struts.) is not available.

    那么我们应该怎样配置才可以解决这种情况呢?

    修改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>
     <constant name="struts.devMode" value="true"/>
        <package name="default" namespace="/" extends="struts-default">
            <action name="hello_struts">
                <result>
                    /Hello.jsp
                </result>
            </action>
        </package>
    </struts>

    这样配置以后,我们修改上面的代码后,就可以很方便的运行程序,不需要重新部署项目

    ------------------------------------Hongten--------------------------------------

    我们来看看这个小程序的运行机制:

      现在有一个客户端在浏览器上面输入一个url地址:http://localhost:1000/struts2_0100_introduction/hello

    当他输入这个url地址之后,这个url请求会通过http协议发送给我们的tomcat服务器;

    tomcat收到这个请求之后,会看,这个请求的是哪个web application呢?是这个struts2_0100_introduction

    这个web application,所以他就把这个web application交给我们对应的程序去处理,这是他就会去读取

    struts2_0100_introduction这个web application中的web.xml配置文件。这是就发现我们在web.xml中的一个

    配置:

    <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.xml中配置了一个filter,而这个filter会过滤所有的url地址,所以当url地址中的hello就会被

    filter过滤掉,被org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter接收到;

    他会怎样处理呢?

    他会首先看namespace:“/”

    然后到struts.xml中去查,当中有一个namespace="/"

    在url中的  "/"  后面是:hello

    接下来会在去查这个 "/"  下面有没有action name="hello"

    如果有,就会执行result中的

    /Hello.jsp,  就把Hello.jsp的结果显示在浏览器上或者客户端的浏览器上面;

    ------------------------------------Hongten--------------------------------------

    总结:

    有人可能会问了,我们为什么要搞这么复杂啊?

    我们直接访问Hello.jsp不就完事儿了吗!不是挺爽的吗?为什么搞这么麻烦?

    想一想,struts2这样做肯定有他这样做的好处,你说是不是啊?

    我们原来学过设计模式,设计模式有一个典型 的特点:就是一定要把简单的东西给他复杂化

    他为什么要这样复杂化呢?复杂化后带来的好处是什么?

    最重要的是就是扩展性很好,而struts2带来的好处也是一样的,他为什么搞这么麻烦?

    中间要经过一次中转,一次请求来了之后,要经过一次中转才可以拿到我们想要的东西

    他的好处是可以把请求和最后要拿到的结果(视图)给他分开,而不像原来那样之间写死。

    分开之后的好处是:如果我们现在要换成其他视图如:Hello1.jsp

    我们可以直接在struts.xml中修改:

    <result>

      /Hello1.jsp

    </result>

     这样就可以了,这样会更灵活,这就是她的好处

    所以struts解决的问题就是:把请求和视图分开

    ------------------------------------Hongten--------------------------------------

    感谢:尚学堂-马士兵

    ------------------------------------Hongten--------------------------------------

  • 相关阅读:
    bzoj 3035 二分答案+二分图最大匹配
    bzoj 1058 bst
    bzoj 1093 缩点+DP
    bzoj 1452 二维树状数组
    bzoj 1968 数学
    bzoj 1034 贪心
    牛客小白月赛12 I (tarjan求割边)
    Loj 103、10043 (KMP统计子串个数)
    poj 2356 (抽屉原理)
    hdu 1907 (尼姆博弈)
  • 原文地址:https://www.cnblogs.com/hongten/p/2121194.html
Copyright © 2020-2023  润新知