1.namespace、action实现、路径问题(我的struts2笔记)

这里主要内容是:

  • struts2入门
  • namespace
  • action实现
  • 路径问题

一、struts2入门

这里我们使用的是struts2的2.3.24版本。

1.新建一个web工程(Struts2_0100_Introduction)

2.将struts-2.3.24.1\apps\struts2-blank.war解压出来,从这个例子工程中我们可以得到最少量的依赖包和相关的配置文件(struts.xmlweb.xml

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>
    <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
    <include file="example.xml"/> -->
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result>
                /hello.jsp
            </result>
        </action>
    </package>

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2_0100_Introduction</display-name>
  <welcome-file-list>
    <welcome-file>index.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>

hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Hello World</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    Hello Struts2<br>
  </body>
</html>

然后我们将此工程部署到服务器中,使用地址http://localhost:8080/Struts2_0100_Introduction/hello进行访问。

注意:web.xml中最基本的配置就是配置struts2的过滤器,也就是StrutsPrepareAndExecuteFilter。同时映射地址一般为"/*",表示映射所有。

说明:这是一个最基本的struts2工程,下面我们将依次说明其中的各个配置标签等内容。

二、namespace(工程Struts2_0200_Namespace

下面我们先给出相关的工程文件(没有给出的表示和之前的一样),之后再详细说明(有些说明直接写在了文件中)
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>
    <constant name="struts.devMode" value="true" />
    <package name="front" namespace="/front" extends="struts-default">
        <action name="index">
            <result>
                /namespace.jsp
            </result>
        </action>
    </package>
    <package name="main" extends="struts-default" namespace="">
        <action name="index">
            <result>/namespace.jsp</result>
        </action>
    </package>

</struts>

namespace.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>namespace</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    some information<br>
  </body>
</html>

说明:
1.package用于打包,以便于和别的包进行区分,比如上面两个包中都有index这个action,这时我们需要进行区分。

2.namespace决定了之后我们的访问路径,默认是为空的(" "),这是我们可以使用http://localhost:8080/struts2_0200_Namespace/xxx/xxx/index或者其他随便什么地址进行访问(即xxx可以是任意的),只要最后是index即可,都可以访问到其下面配置的action。一般用于覆盖其他特定路径没有涵盖到的路径。而上面所说的打包中有多个同名的action也是通过namespace进行区分访问的。

当然我们有时候会将namespace配置为特定的值,如同例子中的/front一样。这时就必须使用地址http://localhost:8080/struts2_0200_Namespace/front/index进行访问了。而如果是"/",则必须使用http://localhost:8080/struts2_0200_Namespace/index进行访问。

三、action实现(工程Struts2_0300_Action

这里我们先给出响应的代码,在后面进行详细说明。这里不再给出jsp文件,我们可以随便写点什么都行。
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>
    <constant name="struts.devMode" value="true" />
    <package name="front" namespace="/" extends="struts-default">
        <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">
            <result name="success">
                /ActionIntroduction.jsp
            </result>
        </action>
    </package>
</struts>

这里给出的action是IndexAction1,对于其他几个action类的试验只需要在这里将名字换掉即可。

IndexAction1.java

package com.bjsxt.struts2.front.action;
public class IndexAction1 {
    public String execute(){
        return "success";
    }
}

IndexAction2.java

package com.bjsxt.struts2.front.action;
import com.opensymphony.xwork2.Action;
public class IndexAction2 implements Action {
    @Override
    public String execute() throws Exception {
        return "success";
    }
}

IndexAction3.java

package com.bjsxt.struts2.front.action;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction3 extends ActionSupport {

    @Override
    public String execute() throws Exception {
        return "success";
    }
    
}

说明:
1.struts.xml中我们可以看到我们配了一个name="success",这就表示成功的时候调用哪个jsp,这是默认的,可以不配。

2.对于action,在struts2中所有的action都只是一个普通的java类,只要里面有public String execute()方法即可。对于第一个action,我们看到给出了一个默认的execute方法,然后返回值是String类型就可以了。但是我们会发现这种方式针对每个功能都需要我们手动定义实现一个方法,很麻烦。所以这种方式一般不用,当然要用也是可以的。

3.对于第二个action,我们看到实现了Action接口,然后实现了一个默认的execute方法,但是我们会发现这种方式同样麻烦,因为虽然struts都帮我们定义好了方法,但是这些方法却都需要我们自己手动实现。

4.这里我们一般使用第三个Action这种方式,即继承ActionSupport类,这个类帮我们实现好了各个方法,拿过来用即可。

四、路径问题(工程Struts2_0400_Path

同样,我们还是先给出相关代码,后面再详细说明。
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2_0400_Path</display-name>
  <welcome-file-list>
    <welcome-file>index.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>

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>
    <constant name="struts.devMode" value="true" />
    <package name="path" namespace="/path" extends="struts-default">
        <action name="path" class="com.bjsxt.struts2.path.action.PathAction">
            <result name="path">
                /path.jsp
            </result>
        </action>
    </package>
</struts>

PathAction.java

package com.bjsxt.struts2.path.action;
public class PathAction {
    public String execute(){
        return "path";
    }
}

path.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <!-- 试验的时候先不加下面这行 -->
    <base href="${pageContext.request.contextPath}/"/>
    <title>path</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    <a href="index.jsp">index.jsp</a>
  </body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>path</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  </head>
  <body>
    <a href="path/path.action">路径问题说明</a>
  </body>
</html>

说明:
1.当我们使用地址http://localhost:8080/Struts2_0400_Path/访问的时候,这里是直接访问根路径。首先会去web.xml中找到过滤器。然后去找对应的namespace,这里地址中的namespace是一个"/",但是在struts.xml中找的时候却发现没有对应的namespace,于是就交给tomcat处理,使用默认页面index.jsp

2.在index.jsp页面中我们的超链接地址是http://localhost:8080/Struts2_0400_Path/path/path.action,于是就去找对应的namespace="path"和名字为path的action。于是找到了对应的类,类中我们返回一个字符串path,于是在struts.xml中找到结果是"path"的result。于是调用path.jsp

3.在path.jsp中如果没有<base href="${pageContext.request.contextPath}/"/>,当我们点击path.jsp页面中的链接时发现访问不到资源,而且点击后的地址是http://localhost:8080/Struts2_0400_Path/path/index.jsp。照理说,index.jsppath.jsp是在同一个目录下,是可以使用相对路径访问的,但是这里为什么不能呢,地址也不正确?这是因为struts2中路径问题是根据action的路径而不是jsp的路径来确定的,所以尽量不要使用相对路径,而使用绝对路径。这里取得绝对路径的一个好办法是配置<base href="${pageContext.request.contextPath}/"/>,注意:此时我们在超链接中就可以使用index.jsp了,base标签帮我们确定了本页面绝对路径的前面一段,我们只需要配置后面一段即可。同时注意base地址中最后的"/"不要丢了。不要使用MyEclipse中生成的那种路径方法,我们不能让jsp页面中出现java代码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 Struts就是基于mvc模式的框架!(struts其实也是servlet封装,提高开发效率!) Strut...
    奋斗的老王阅读 8,167评论 0 51
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 6,702评论 0 50
  • 详谈 Struts2 的核心概念 本文将深入探讨Struts2 的核心概念,首先介绍的是Struts2 的体系结构...
    可爱傻妞是我的爱阅读 4,826评论 0 2
  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入门3、Struts 2 的执行流程4、配置 st...
    廖少少阅读 8,161评论 3 13
  • 那条路 形单影只 那片美 独赏无喜 朦胧 身影从眼前消失 你 在哪儿? 梦中 你的回眸 黯淡了星辰 温暖了我心 那...
    bearfly阅读 2,615评论 3 5