详解Struts2中配置默认Action的方法
程序员文章站
2023-12-17 08:03:03
一、jsp默认设置
1、当访问的action不存在时,页面会显示错误信息,可以通过配置默认action处理用户异常的操作;
2。配置方法:
在struts.xml文件...
一、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%>" 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默认设置转发
<!-- 默认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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。