默认Action

1、当访问action不存在时,可以通过制定默认action的方式避免出现错误代码页面;

2、使用default-action-ref 指定默认 action。

项目实例

1、项目结构

2、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ray</groupId>
  <artifactId>struts2Test</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>struts2Test Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.16</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>struts2Test</finalName>
  </build>

</project>

3、web.xml

<web-app 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_3_0.xsd"
         version="3.0" metadata-complete="true">
  <display-name>Archetype Created Web Application</display-name>

  <!-- 过滤所有请求交给Struts2处理 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

4、HelloWorldAction.java

package com.ray.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by Ray on 2018/3/26 0026.
 **/
public class HelloWorldAction extends ActionSupport {

    /**
    * @Author: Ray
    * @Date: 2018/3/26 0026
    * @Description: Struts2默认执行的方法
    * @Return: SUCCESS
    */
    @Override
    public String execute() throws Exception {
        return super.execute();
    }
}

5、struts.xml

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

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

<struts>

    <package name="default" namespace="/" extends="struts-default">
        <!-- 默认action -->
        <default-action-ref name="404"/>
        <action name="404">
            <result>/404.jsp</result>
        </action>

        <action name="helloWorld" class="com.ray.action.HelloWorldAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>

    <include file="second-struts.xml"/>
    <include file="third-struts.xml"/>
    <include file="fourth-struts.xml"/>
</struts>

6、404.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>404</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>
    不好啦!您访问的页面不存在了.
</body>
</html>

7、页面效果

7.1 访问存在的action

7.2 访问不存在的action

ok!

第五篇——Struts2的默认Action的更多相关文章

  1. struts2配置默认Action

    作用:当一个请求无法匹配到任何一个struts的action时,可以配置一个默认Action 例如:当请求路径不正确时,跳转到一个404.jsp页面 <package extends=" ...

  2. SSH整合 第五篇 struts2的到来

    struts2的好处,web层的显示,同时Action类相当于MVC模式的C.整合进来的话,是通过与Spring整合,减少重复代码,利用IoC和AOP. 1.struts-2.5.2.jar 以上是s ...

  3. (Struts2学习系列五)Struts2默认action

    当我们访问项目下一个不存在的Action的时候,页面就会报错,404找不到资源,这样对用户来说是非常不友好的,所以我们设置一个默认的Action,当找不到对应Action的时候,就会跳转到默认Acti ...

  4. Struts2的动态Action和全局跳转视图以及配置各项默认值

    1:Struts2的默认访问后缀是.action(特别需要注意的是改了配置文件web.xml或者struts.xml需要重启服务器) 2:Struts2中常用的常量介绍:<!-- 一:全局配置 ...

  5. Struts2之web元素访问与模板包含与默认Action使用

    上一篇为大家介绍了如何使用Action进行参数接收,以及简单的表单验证,本篇为大家介绍一下关于Action访问web元素的三种方式(request.session.application):模板包含( ...

  6. [转]Struts2理解--动态方法和method属性及通配符_默认Action

    众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...

  7. Struts2理解--动态方法和method属性及通配符_默认Action

    众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...

  8. Struts2中配置默认Action

    1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法:    在struts.xml文件中的<package>下添加如下内容: ...

  9. struts2默认Action配置

    在项目中,需要在输入错误的url的时候,弹出友好的错误提示页面 在struts2中可以通过配置默认的action达到这个目的 配置方法: <package name="default& ...

随机推荐

  1. Module 10:I/O流(java如何实现与外界数据的交流)

    Module 10:I/O流(java如何实现与外界数据的交流) Input/Output:指跨越出了JVM的边界,与外界数据的源头或者目标数据源进行数据交换.               输出   ...

  2. nginx default_server的作用

    用来处理没有成功匹配server_name的请求 https://www.oschina.net/question/12_3565 https://segmentfault.com/a/1190000 ...

  3. C#使用xpath简单爬取网站的内容

    public static void Get() { // string xpathtrI = "//*[@id='classify-list']/dl/dd/a/cite/span/i&q ...

  4. thinkcmf 忘记后台登陆密码的解决办法

    thinkcmf 忘记密码 或者 密码错误 如何修改后台登陆密码? 直接在后台登陆控制器里输入 dump(cmf_password('123456')); 参考文件路径 app\admin\contr ...

  5. [破解] IPhone 5S icloud dns bypass

    http://ui.iclouddnsbypass.com/deviceservices/buddy/barney_activation_help_en_us.buddyml http://www.j ...

  6. 我的WafBypass之道(upload篇)

    0x00 前言 玩waf当然也要讲究循序渐进,姊妹篇就写文件上传好了,感觉也就SQLi和Xss的WafBypass最体现发散性思维的,而文件上传.免杀.权限提升这几点的Bypass更需要的是实战的经验 ...

  7. spring-boot 集成 log4j 记录日志

    1.pom文件中移除和添加依赖 <!-- 移除boot—starter 的log4j --> <dependency> <groupId>org.springfra ...

  8. loadrunner笔记(三):设置、运行场景和生成测试报告

    //上一篇的代码有点问题,问题出在 web_reg_find()函数中,这个函数简单的说是搜索下一步操作的请求对象(html)页面中是否存在相应的文本字符串.所以用在登录操作中,它搜索的是主页.htm ...

  9. Java Web 笔试(面试)题

    1.Servlet 的生命周期,并说出 Servlet 与 CGI 的区别 Web 容器加载 Servlet 并将其实例化后,Servlet 生命周期开始,容器运行其 init 方法进行 Servle ...

  10. Codeforces 659 - A/B/C/D/E/F/G - (Undone)

    链接:https://codeforces.com/contest/659 A - Round House - [取模] AC代码: #include<bits/stdc++.h> usi ...