• Struts第一个程序。


    1:创建完程序后。先写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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
      <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:加入jar包。

    3:写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>
    
        <!--  
            package: 包. struts2 使用 package 来组织模块. 
            name 属性: 必须. 用于其它的包应用当前包. 
            extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
                     struts-default 这个包在 struts-default.xml 文件中定义.
            namespace 可选, 如果它没有给出, 则以 / 为默认值. 
                                    若 namespace 有一个非默认值, 则要想调用这个包里的Action, 
                                    就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里
                                    
                      http://localhost:8080/contextPath/namespace/actionName.action
        -->
        <package name="helloWorld" extends="struts-default">
            
            <!-- 
                配置一个 action: 一个 struts2 的请求就是一个 action 
                name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
                class 的默认值为: com.opensymphony.xwork2.ActionSupport
                method 的默认值为: execute
                result: 结果. 
            -->
            <action name="zhuyemian-dao-struts" >
                <!--  
                    result: 结果. 表示 action 方法执行后可能返回的一个结果. 所以一个 action 节点可能会有多个 result 子节点.
                    多个 result 子节点使用 name 来区分
                    name: 标识一个 result. 和 action 方法的返回值对应. 默认值为 success
                    type: 表示结果的类型. 默认值为 dispatcher(转发到结果.)
                -->
                <result>/pages/input.jsp</result>
            </action>
            
            <action name="product-save" class="com.struts2.helloworld.Product"
                method="save">
                <result name="details">/pages/details.jsp</result>    
            </action>
            
        </package>
    
    </struts>
    <body>
        <a href="zhuyemian-dao-struts.action">Product Input</a>
      </body>

    struts跳转到jsp上。

    方法名对应的就是action中的method

    return对应的就是return中的name。

    知识点1:namespace   访问的时候要在项目名的后面,不然会出404错误

     Strutst2会为每一个HTTP请求创建一个新的Action实例。即Action不是单例的。是线程安全的。

  • 相关阅读:
    elementui form-item中多个字段校验
    Element-ui的 el-form 使用 v-if校验失灵问题
    fullcalendar title换行
    html拼接
    element-ui cascader 省市区 动态加载
    切换vue项目初始化路径
    用JavaScript获取当月第一天和最后一天
    小宝和小黑
    python目录
    3
  • 原文地址:https://www.cnblogs.com/bulrush/p/7533299.html
Copyright © 2020-2023  润新知