欢迎光临散文网 会员登陆 & 注册

namespace,运行action的2种方法,struts.xml加载和分模块配置,包配置【诗书画唱】

2021-01-04 23:46 作者:诗书画唱  | 我要投稿

本期内容概括:

namespace

运行action的方法

struts.xml加载和分模块配置

包配置

action启动(比如跳转界面)方法1

action启动方法2

视频友情推荐

运行action的方法START

运行action的方法END


struts.xml加载和分模块配置START

一般情况下,namespace将action分成逻辑上的不同模块,每一个模块有自己独立的前缀。使用namespace可以有效的避免action重名的冲突,struts2标签带有namespace选项,可以根据namespace的不同向服务器提交不同的package的action的请求。但是以下特殊情况:

1、namespace的值为“/”时,如果输入的action在别的命名空间没有找到,则最终会执行该命名空间下的action。

2、namespace的值为“”时,或者namespace属性省略不写时,跟namespace的值为“/”一样。









struts.xml加载和分模块配置END




包配置PPT START






包配置PPT END


讲义START


struts2框架的配置方法1:

1、导入框架包

2、导入核心配置文件struts.xml(这个文件名不能够更改),放在src目录下

3、web.xml文件添加一个struts2框架的过滤器


struts2框架的配置方法2:

1、导入框架包

2、将配置文件放在WebContent/WEB-INF目录下,文件名可以自己取(必须是xml)

3、web.xml文件添加一个struts2框架的过滤器,并且通过<init-param>标签指定配置文件的路径



讲义END


视频START


视频END

例子START

namespace:


action启动(比如跳转界面)方法1:固定的struts.xml   START:



——————


http://localhost:8080/STRUTS2fenMoKuaiJavaWeb/testAc.action

————

package com.jy.action;


public class TestAction {

//http://localhost:8080/J1908021/testAc.action

    public String execute(){

    return "success";

    }

}

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software 

Foundation//DTD Struts Configuration 2.1//EN" 

"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <!-- 加载user.xml中的配置到项目中 -->

    <include file="com/jy/action/user.xml"></include>  

</struts>

<?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_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>J1908021</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.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>

</web-app>

<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">

        <title></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">

    </head>

    <body>

        <h1>Hello world</h1>

    </body>

</html>

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <!-- name就是包的名字,方便其他的包进行引用的,

                  整个项目中的name属性必须是唯一的 -->

    <!-- namespace就是避免调用同名的action时产生歧义的属性 -->

    <!-- extends属性表示当前的包继承自哪个name属性的包 -->

    <package name="user" namespace="/pub" extends="struts-default">

        <action name="regAc" class="com.jy.action.UserAction">

            <result name="success">/reg.jsp</result>

        </action>

    </package> 

    <package name="test" namespace="/" extends="struts-default">

        <action name="testAc" class="com.jy.action.TestAction">

            <result name="success">/Hello.jsp</result>

        </action>

    </package> 

</struts>





package com.jy.action;


public class UserAction {

//http://localhost:8080/J1908021/pub/regAc.action

    public String execute(){

    return "success";

    }

}



action启动(比如跳转界面)方法1:固定的struts.xml  END


action启动方法2:自定义的XXX.xml  START


package com.jy.action;


public class HelloAction {

//http://localhost:8080/J1908022/u/hlAc.action

    public String execute(){

    System.out.println("Hello world");

    return null;

    }

}



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>   

</struts>


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <package name="user" namespace="/u" extends="struts-default">

        <action name="hlAc" class="com.jy.action.HelloAction"></action>

    </package>

</struts>



<?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_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>J1908022</display-name>

  <!-- struts2框架的配置 -->

  <filter>

      <filter-name>struts2</filter-name>

      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

      <!-- 加载指定位置的struts2框架的配置文件 -->

      <init-param>

          <param-name>config</param-name>

          <param-value>

              struts-default.xml,

              ../struts_config/user.xml,

              ../struts_config/product.xml

          </param-value>

      </init-param>

  </filter>

  <filter-mapping>

      <filter-name>struts2</filter-name>

      <url-pattern>/*</url-pattern>

  </filter-mapping>

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

</web-app>




action启动方法2:自定义的XXX.xml  END


例子END



namespace,运行action的2种方法,struts.xml加载和分模块配置,包配置【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律