Struts2学习六----------默认Action
2024-08-27 16:15:15
© 版权声明:本文为博主原创文章,转载请注明出处
默认Action
- 当访问action不存在时,可通过指定默认action的方式避免出现错误代码页面
- 使用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>org.struts</groupId>
<artifactId>Struts2-DefaultAction</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2-DefaultAction Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<struts.version>2.5.10</struts.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies> <build>
<finalName>Struts2-DefaultAction</finalName>
</build> </project>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
4.HelloAction.java
package org.struts.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception { return SUCCESS; } }
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="hello" extends="struts-default" namespace="/">
<!-- 默认action -->
<default-action-ref name="404"/>
<action name="404">
<result>/404.jsp</result>
</action>
<action name="hello" class="org.struts.action.HelloAction">
<result>/success.jsp</result>
</action>
</package> </struts>
6.success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>success.jsp</title>
</head>
<body>
您好!欢迎访问success.jsp
</body>
</html>
7.404.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>404</title>
</head>
<body>
不好啦!您访问的页面不存在了.
</body>
</html>
8.效果预览
8.1 访问存在的action
8.2 访问不存在的action
参考:http://www.imooc.com/video/9001
最新文章
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
- 一个人的 ClojureScript 技术栈
- IDA来Patch android的so文件
- finder文件目录跳转快捷键
- Excel Interactive View - 让页面表格的功能更强大
- linux之cut用法
- NUll在oracle与sqlserver中使用相同与区别
- 面向对象继承实例(a如何继承b)
- hdu_5868:Different Circle Permutation
- WCF使用纯代码的方式进行服务寄宿
- kafka相关问题集锦
- First Python script
- Docker下载mysql镜像
- 四种常见的 POST 提交数据方式对应的content-type取值
- js格式化文件大小, 输出成带单位的字符串工具
- BEM
- MVC,重写AuthorizeAttribute实现自己的权限验证(一)
- bootstrap-treeview 自定义实现双击事件
- redis 发布/订阅 模式
- 基于Token的身份验证--JWT