当我们访问项目下一个不存在的Action的时候,页面就会报错,404找不到资源,这样对用户来说是非常不友好的,所以我们设置一个默认的Action,当找不到对应Action的时候,就会跳转到默认Action指定的页面。
在配置Action的helloworld.xml中要这样配置默认Action:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default" > <default-action-ref name="index"></default-action-ref> <action name="index"> <result>/error.jsp</result> </action> <action name="*_*_*" method="{2}" class="{3}.{1}Action"> <result>/result.jsp</result> <result name="add">/{2}.jsp</result> <result name="update">/{2}.jsp</result> </action> </package> </struts>
访问url:http://localhost:8080/struts2Test2/ddd.action,没配置默认Action之前会报错,配置之后就能访问到error.jsp页面了。
至于其他的资源访问,则输入的url格式为:(http://localhost:8080/项目名/Action的类名_method名_包名.action)
http://localhost:8080/struts2Test2/HelloWorld_update_action.action
http://localhost:8080/struts2Test2/HelloWorld_add_action.action