Struts2中配置默认Action

一、jsp默认设置

1、当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作;

2。配置方法:
在struts.xml文件中的下添加如下内容:

    <default-action-ref name="index"></default-action-ref>

其中index为默认Action的name属性值;

3、配置默认Action后,相应的namespace下不存在要访问的Action时,自动跳转到默认Action处理。

4、实例

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd"><welcome-file-list><welcome-file>hello.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>

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /><constant name="struts.devMode" value="false" /><include file="example.xml"/><package name="default" namespace="/" extends="struts-default"><default-action-ref name="index" /><action name="index"><result type="redirectAction"><param name="actionName">HelloWorld</param><param name="namespace">/example</param></result></action></package>--><!-- Add packages here --><constant name="struts.devMode" value="true" /><constant name="struts.i18n.encoding" value="GBK"></constant><package name="user" namespace="/" extends="struts-default"><default-action-ref name="index"></default-action-ref><action name="index"><result>/index.jsp</result></action></package>
</struts>

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>Index</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>Welcome to Magci's BLOG!<br /><a href="magci/magc">magci/magc</a><br /><a href="mgc/magc/magci/123456">mgc/magc/magci/123456</a><br /></body>
</html>

二、action默认设置转发

<!-- 默认action --><default-action-ref name="index" /><action name="index"><result type="redirectAction"><param name="actionName">page_toIndex</param><!-- <param name="namespace">/example</param> --></result></action> <action name="page_*" class="indexAction" method="{1}"><result name="toIndex">/WEB-INF/jsps/index.jsp</result><result name="toAdminLogin">/WEB-INF/jsps/admin/admin_login.jsp</result><!-- ajax --><result name="ajaxInsertOneCallback" type="json"><param name="root">action</param></result></action>

Struts2中配置默认Action相关推荐

  1. 【Java】Struts2配置默认Action和配置Action的默认处理类

    配置默认Action <package> <default-action-ref name="index"/> <action name=" ...

  2. 容器编排技术 -- Kubernetes 在 Namespace 中配置默认的CPU请求与限额

    容器编排技术 -- Kubernetes 在 Namespace 中配置默认的CPU请求与限额 1 Before you begin 2 创建一个命名空间 3 创建一个LimitRange和一个Pod ...

  3. struts2配置默认Action

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

  4. 在struts2中配置自定义拦截器放行多个方法

    源码: 自定义的拦截器类: //自定义拦截器类:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet ...

  5. Struts2注解配置之@Action(二)

    当注解配置能够简化繁杂的xml,你是否想过也要使用注解? 最近对原有项目的框架使用的Struts2进行升级.很早以前就想尝试注解的方式进行配置.但是由于项目稳定性和改配置方式可能带来的问题一直没有进行 ...

  6. Geronimo3.0中配置默认的security

    在Geronimo服务器中,基本的安全配置分成两个插件: j2ee-security和server-security-config.其中jaac provider和keystore manager在j ...

  7. php配置默认index.php,Apache的vhost中配置默认访问入口index-test.php的方法(Yii)

    最近的参与的Yii项目有多个分支,所以在入口文件里面有区分(index.php index-test.php index-beta.php)等.不同的入口文件对应不同的环境和配置. 这个时候在本地建立 ...

  8. vsftp中配置默认目录

    如果是默认安装vsftpd的话,以下是一些文件的位置约定: /usr/sbin/vsftpd ---- VSFTPD的主程序 /etc/rc.d/init.d/vsftpd ---- 启动脚本 /et ...

  9. struts2中页面访问action的url问题,或许很简单

    http://www.iteye.com/problems/11940 转载于:https://www.cnblogs.com/diyunpeng/p/5471032.html

最新文章

  1. prototype中顶层元素的测试
  2. leetcode day4
  3. js DOM Element属性和方法整理
  4. PHP获取今天, 本周 ,半月 ,本月 ,本季 ,本年,昨天 ,上月时间段
  5. H3C 基于IP的限速
  6. 尽显中国风 | 高品质海报背景,PSD分层,智能替换展示商品
  7. pom模块依赖关系梳理
  8. 主动FTP vs. 被动FTP 权威解释
  9. 浏览器安装fehelper插件,亲测可行
  10. 正好股票开户指数大跌分解比较严重
  11. JS 三级联动之 省市县。
  12. 第四周作业上(2014.3.31)
  13. 忘记 Apple ID 密码?重设 Apple ID 密码的 3 种方法
  14. Hive窗口函数之preceding and following
  15. 机器学习---推荐系统效果评估NDCG
  16. RabbitMq(五) -- 死信队列和延迟队列
  17. snmp在win10上安装使用
  18. Android开发——如何设计开发一款Android App
  19. 青瓷游戏递表港交所:收入猛增,毛利率走低,研发占比不值一提
  20. 【C++】小而快的Ninja

热门文章

  1. _tcscpy_s函数引发的问题
  2. VC中获取窗口句柄的各种方法
  3. Linux 设备模型之 (kobject、kset 和 Subsystem)(二)
  4. 关于点击率模型,你知道这三点就够
  5. c matlab 混合编程 调试,64位MATLAB和C混合编程以及联合调试
  6. 区块链BaaS云服务(21)腾讯CCGP“跨链服务”
  7. [FF-A]-01-Introduction
  8. 职高计算机专业可以考本科吗,职业高中和普通高中的区别 职业高中能考本科吗...
  9. 【攻防世界001】Guess-the-Number
  10. 向函数传递一维数组和已知大小的二维数组