Struts2 配置默认action

Struts2 配置默认action

接下来的项目是演示当我们点击index.jsp页面链接时,页面能够通过action处理跳转到main.jsp页面(链接的地址为 http://localhost:8080/Struts2Week/user/login.action)。当我们在地址栏输入 http://localhost:8080/Struts2Week/user/l.action 时这个处理的action并没有,所有它会跳转到我们默认的action,通过该action处理跳转到default.jsp页面。

配置默认action代码:

	<!--如果请求的路径不存在则跳转到该页面 -->
	<package name="default" extends="struts-default">
		<default-action-ref name="def"></default-action-ref>
		<action name="def">
			<result>/WEB-INF/default.jsp</result>
		</action>
	</package>

使用版本:struts-2.3.24-all

下面是演示测试项目结构:



准备jar包(log4j.jar为日志包),并将所有的jar包加载到项目中(全选右键---> Build Path ---> Add to Build Path)。

     

代码如下:

1、UserAction.java 

package cn.sz.action;

public class UserAction {
	public String login() {
		return "success";
	}
}


2、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>Struts2Week</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>front</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>front</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
</web-app>


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>
	<package name="user" extends="struts-default" namespace="/user">
		<action name="login" class="cn.sz.action.UserAction" method="login">
			<result>/main.jsp</result>
		</action>
	</package>

	<!--如果请求的路径不存在则跳转到该页面 -->
	<package name="default" extends="struts-default">
		<default-action-ref name="def"></default-action-ref>
		<action name="def">
			<result>/WEB-INF/default.jsp</result>
		</action>
	</package>
</struts>


4、 default.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></title>
</head>
<body>
	<h1>default界面</h1>
</body>
</html>

5、index.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></title>
</head>
<body>
	<h1>这是index页面</h1>
	<a href="user/login.action">测试struts</a>
	<!--user表示的命名空间,login.action表示请求的action  -->
</body>
</html>


完成代码后将项目部署到服务器,在游览器地址栏输入http://localhost:8080/Struts2Week/user/l.action 将发现页面跳转到default.jsp页面。因为我们l.action并不存在,所以它使用默认的action处理。

效果图:







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值