Struts2中配置默认Action

栏目: Java · 发布时间: 8年前

内容简介:Struts2中配置默认Action

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机组成:结构化方法

计算机组成:结构化方法

坦嫩鲍姆 / 刘卫东 / 人民邮电出版社 / 2006-1 / 65.00元

本书采用结构化方法来介绍计算机系统,书的内容完全建立在“计算机是由层次结构组成的,每层完成规定的功能”这一概念之上。作者对本版进行了彻底的更新,以反映当今最重要的计算机技术以及计算机组成和体系结构方面的最新进展。书中详细讨论了数字逻辑层、微体系结构层、指令系统层、操作系统层和汇编语言层,并涵盖了并行体系结构的内容,而且每一章结尾都配有丰富的习题。 本书适合作为计算机专业本科生计算机组......一起来看看 《计算机组成:结构化方法》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具