Struts2的访问路径四:使用动态路径访问(不推荐使用)

好记性不如赖笔头……

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" 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>Struts_Template</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、在WebRoot目录下创建index.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>My JSP 'index.jsp' starting page</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>
    <a href="${pageContext.request.contextPath }/book!addBook.action">添加书籍</a>
    <br>
    <a href="${pageContext.request.contextPath }/book!findBook.action">查找书籍</a>
    <br>
    <a href="${pageContext.request.contextPath }/book!updateBook.action">更新书籍</a>
    <br>
    <a href="${pageContext.request.contextPath }/book!deleteBook.action">删除书籍</a>
    <br>
  </body>
</html>

3.在WebRoot目录下创建success.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>My JSP 'success.jsp' starting page</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>
   操作成功<br>
  </body>
</html>

4、创建BookAction.java文件

package com.ckinghan.web.action;

public class BookAction {

    public String addBook(){
        System.out.println("添加书籍成功");
        return "success";
    }

    public String findBook(){
        System.out.println("查找书籍成功");
        return "success";
    }

    public String updateBook(){
        System.out.println("修改书籍成功");
        return "success";
    }

    public String deleteBook(){
        System.out.println("删除书籍成功");
        return "success";
    }
}

5、在src目录下创建并配置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"></constant>
    <!--    开启动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

    <package name="bookController" extends="struts-default">

<!--        
        使用动态方法调用 :
        注意:必须将动态方法调用打开。这里的访问路径可以固定一个路径名称,这里固定为book(name="book"),并且没有指定method方法

        访问的路径格式为:book!addUser.action或book!addUser(访问的路径!访问的方法.action)

        访问路径范例:http://localhost:8080/Struts_Demo6/book!addBook.action

        不建议使用此方法,个人认为通配符的方法更好,官网也是推荐使用通配符的方式
-->
        <action name="book" class="com.ckinghan.web.action.BookAction">
            <result name="success">/success.jsp</result>
        </action>

    </package>

</struts>

使用动态配置路径 注意以下几点:
1、访问路径与其它的方式不同(例如:http://localhost:8080/test/book!addBook.action
2、在struts.xml文件中,不指定method方法,因为它会自动去查找方法
3、使用动态配置,要先开启方可使用,开启的代码如下:

<!--    开启动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

访问后的结果如下:

这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值