Struts2中配置默认Action
生活随笔
收集整理的這篇文章主要介紹了
Struts2中配置默认Action
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Struts2中配置默認Action
一、jsp默認設置
1、當訪問的Action不存在時,頁面會顯示錯誤信息,可以通過配置默認Action處理用戶異常的操作;
2。配置方法:
在struts.xml文件中的下添加如下內容:
其中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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts2 s:if标签以及 #,%
- 下一篇: java定时器无法自动注入的问题解析(原