国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

詳解Struts2中配置默認(rèn)Action的方法

2024-07-13 10:16:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、jsp默認(rèn)設(shè)置

1、當(dāng)訪問(wèn)的Action不存在時(shí),頁(yè)面會(huì)顯示錯(cuò)誤信息,可以通過(guò)配置默認(rèn)Action處理用戶異常的操作;

2。配置方法:

在struts.xml文件中的下添加如下內(nèi)容:

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

其中index為默認(rèn)Action的name屬性值;

3、配置默認(rèn)Action后,相應(yīng)的namespace下不存在要訪問(wèn)的Action時(shí),自動(dòng)跳轉(zhuǎn)到默認(rèn)Action處理。

4、實(shí)例

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%>" rel="external nofollow" > <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" rel="external nofollow" > --> </head> <body> Welcome to Magci's BLOG!<br /> <a href="magci/magc" rel="external nofollow" >magci/magc</a><br /> <a href="mgc/magc/magci/123456" rel="external nofollow" >mgc/magc/magci/123456</a><br /> </body></html>

二、action默認(rèn)設(shè)置轉(zhuǎn)發(fā)

<!-- 默認(rèn)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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 建瓯市| 娱乐| 海林市| 元谋县| 鞍山市| 常宁市| 陈巴尔虎旗| 定陶县| 镇平县| 青冈县| 广元市| 大安市| 林口县| 娄底市| 济宁市| 河源市| 洛浦县| 双鸭山市| 淄博市| 肥西县| 兴和县| 芦溪县| 兰州市| 安吉县| 和林格尔县| 山阴县| 芒康县| 饶平县| 临汾市| 遂平县| 扬中市| 酒泉市| 东宁县| 枣强县| 宜君县| 晋宁县| 永城市| 沅江市| 澄城县| 华亭县| 辽源市|