第五篇——Struts2的默认Action
默认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的更多相关文章
- struts2配置默认Action
作用:当一个请求无法匹配到任何一个struts的action时,可以配置一个默认Action 例如:当请求路径不正确时,跳转到一个404.jsp页面 <package extends=" ...
- SSH整合 第五篇 struts2的到来
struts2的好处,web层的显示,同时Action类相当于MVC模式的C.整合进来的话,是通过与Spring整合,减少重复代码,利用IoC和AOP. 1.struts-2.5.2.jar 以上是s ...
- (Struts2学习系列五)Struts2默认action
当我们访问项目下一个不存在的Action的时候,页面就会报错,404找不到资源,这样对用户来说是非常不友好的,所以我们设置一个默认的Action,当找不到对应Action的时候,就会跳转到默认Acti ...
- Struts2的动态Action和全局跳转视图以及配置各项默认值
1:Struts2的默认访问后缀是.action(特别需要注意的是改了配置文件web.xml或者struts.xml需要重启服务器) 2:Struts2中常用的常量介绍:<!-- 一:全局配置 ...
- Struts2之web元素访问与模板包含与默认Action使用
上一篇为大家介绍了如何使用Action进行参数接收,以及简单的表单验证,本篇为大家介绍一下关于Action访问web元素的三种方式(request.session.application):模板包含( ...
- [转]Struts2理解--动态方法和method属性及通配符_默认Action
众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...
- Struts2理解--动态方法和method属性及通配符_默认Action
众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...
- Struts2中配置默认Action
1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法: 在struts.xml文件中的<package>下添加如下内容: ...
- struts2默认Action配置
在项目中,需要在输入错误的url的时候,弹出友好的错误提示页面 在struts2中可以通过配置默认的action达到这个目的 配置方法: <package name="default& ...
随机推荐
- 乾坤合一~Linux设备驱动之终端设备驱动
多想拥你在我的怀里 却无法超越那距离 美好回忆渐渐地远去 盼望今生出现奇迹 无尽的想念 荒了容颜 无助的爱恋 从未改变 这是今天的旋律,,,,今生今世,遥不可及~ 1 终端设备 终端是一种字符型设备, ...
- 阻止a链接跳转方法总结
总结下a标签阻止默认行为的几种简单方法(1) <a href="javascript:void(0);" > 点我 </a> onclick方法负责执行js ...
- 抖音圈圈乐 系统搭建H5微信小游戏圈圈乐系统介绍
网红线下游戏抖音圈圈乐改造而来 一.搭建此系统需要准备如下资料: 1. 认证微信服务号 2. 微信支付商户号 3. 备案域名及云服务器 二.系统功能简介: 1. 游戏闯关 2. 每个商品闯关难度后台自 ...
- js上传
有时候需要显示进度,这时候就需要做一些切割,具体上传代码如下: <!DOCTYPE HTML> <html lang="en-US"> <head&g ...
- 编译wxWidgets
打开x64 Native Tools Command Prompt for VS 2017 cd wxWidgets-2.9.5\build\msw nmake -f makefile.vc TARG ...
- Redis 主从配置(Windows版)
安装从库 1.复制一份 Redis 文件,当做从库. 2.修改从库文件中 redis.windows.conf 的端口号. 3.安装服务,需要重新设置名称.然后去服务中,开启“redis6380”(此 ...
- linux Ubuntu系统安装百度aip
1.下载百度api pip install baidu-aip 2.配置视频转码工具ffmpeg Ubuntu16.04下安装FFmpeg(超简单版) 第一步:添加源. sudo add-apt-re ...
- poj 3348
裸的凸包,,我就来粘一下板子 把jls和dls的代码混合了一下(雾),感觉我要是能把camp里的几何掌握的差不多就稳 了.(mxy到底打了什么?) 感觉叉积实在是个好东西qwq 今晚c ...
- ArcGIS AddIN 10.3项目与10.1项目的相互转换
1. .sln修改 用记事本打开.sln文件,在最上面将10的I昂么修改为12的项目 将下图 修改为下图 反之亦然 2. .csproj项目修改 用记事本打开.csproj文件, vs 2010文件如 ...
- what is spring-cloud
什么是Spring-cloud ? Spring Cloud是一系列框架的集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...