Struts2学习第四天——全局结果,动态结果及异常映射

时间:2021-01-01 11:30:23

1.异常映射的配置

当Action方法出错时Struts会返回异常错误信息页面,这种页面方式是不友好的,可以使用try-catch捕捉异常,然后在catch块中返回对应的错误页面。这种为单个<action>设置异常映射的方式称为局部异常映射。如果要让所有的<action>的各种异常发生时统一跳转到错误页面需要通过 <global-exception-mappings>设置全局异常映射。配置部分:

<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
       <exception-mapping result="error2" exception="java.sql.SQLException"></exception-mapping>
</global-exception-mappings>

在该配置中,如果该包发生java.lang.Exception异常将寻找<result>name="errror"跳转到对应的页面

2.全局结果的配置

每个Action中都可能遇到错误,此时为使页面友好,都需要跳转到一个公共页面,比较直观的方法是在struts.xml中为每个<action>都配置一个跳转到公共页面的<result>,但是这种做法在<action>多的情况下工作量很大。最好的办法是把<action>中相同的<result>提取出来,设置成全局结果。

代码部分:

<package name="base" abstract="true" extends="struts-default">
<global-results>
<result name="login">/page/login.jsp</result>
<result name="error">/page/comm/error.jsp</result>
<result name="error2">/page/comm/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
<exception-mapping result="error2" exception="java.sql.SQLException"></exception-mapping>
</global-exception-mappings>
</package>

上面的<package>base中设置了<global-results>这意味着在base包中所有的<action>中都有一个<result>name="error"。

抽象的包base继承 struts-default下面的包继承本包拥有了struts-default的配置和本包配置,其他包可以继承base包来达到不同页面不同包错误时跳转到相同页面。

 <package name="default" namespace="/" extends="base">
<action name="*HouserUserAction" class="com.house.action.HouserUserAction" method="{1}">
<result name="register_success">/page/login.jsp</result>
<result name="register_err">/page/regs.jsp</result>
<!-- 指定Action -->
<result name="login_success" type="redirectAction">
<param name="namespace">/house</param>
<param name="actionName">listHouserAction</param>
</result>
<result name="login_err">/page/login.jsp</result>
<!-- 动态结果 -->
<result name="input">/page/${inputResult}</result>
</action>
</package>
<package name="house" namespace="/house" extends="base">
<action name="*HouserAction" class="com.house.action.HouserAction" method="{1}">
<result>/page/guanli.jsp</result>
</action>
</package>                                                                                                                                                                                                                                                                                                 

3.动态结果

在配置<result>时,如果结果页面在配置时并不能确定,需要在运行时才能确定,例如在登陆和注册时使用数据验证失败时返回各自的初始页面<result>中只有一个name=“input”,这个时候需要使用动态结果来设置。

 public class HouserUserAction extends ActionSupport{
private User user;
private String inputResult;
public void validateLogin(){
if(user!=null){
if(user.getUname()==null||user.getUname().equals("")){
super.addFieldError("uname_err","用户名必须填写");
inputResult="login.jsp";
}
if(user.getUpwd()==null||user.getUpwd().equals("")){
super.addFieldError("upwd_err","密码必须填写");
}
}else{
super.addFieldError("login_err","用户信息不得为空");
}
}
public void validateReg(){
if(user!=null){
if(user.getUname()==null||user.getUname().equals("")){
super.addFieldError("uname_err","用户名必须填写");
inputResult="regs.jsp";
}
if(user.getUpwd()==null||user.getUpwd().equals("")){
super.addFieldError("upwd_err","密码必须填写");
}
}else{
super.addFieldError("login_err","用户信息不得为空");
}
} private UserService uService=new UserServiceImpl();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} public String getInputResult() {
return inputResult;
}
public void setInputResult(String inputResult) {
this.inputResult = inputResult;
}
/**
* 处理login请求
* @return
*/
public String login(){
User users=uService.doLogin(user.getUname(), user.getUpwd());
if(users==null){
return "login_err";
}
//request,session,application
//获得上下文
ActionContext context=ActionContext.getContext();
//保存在session范围
Map sessionMap=context.getSession();
sessionMap.put("session_user", users);
return "login_success";
}
/**
* 处理reg请求
* @return
*/
public String reg(){
//制造错误测试exception-mapping
//User user=null;
int ret=uService.addUser(user);
if(ret>0){
return "register_success";
}
return "register_err";
}
}

在struts.xml配置如下

<!-- 动态结果 -->
<result name="input">/page/${inputResult}</result>

配置文件中使用表达式$(),有点类似el标签但是其属于OGNL表达式。

4.数据模块拆分

在大中型项目中,struts.xml文件将会变得非常庞大,难于管理。这时可以把struts.xml按照模块拆分成多个配置文件,并把他们放在rsc下,然后再使用<include>把它们组合在一起。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="base" abstract="true" extends="struts-default">
<global-results>
<result name="login">/page/login.jsp</result>
<result name="error">/page/comm/error.jsp</result>
<result name="error2">/page/comm/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
<exception-mapping result="error2" exception="java.sql.SQLException"></exception-mapping>
</global-exception-mappings>
</package>
<include file="struts-house.xml"></include>
<include file="struts-user.xml"></include>
</struts>
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="default" namespace="/" extends="base">
<action name="*HouserUserAction" class="com.house.action.HouserUserAction" method="{1}">
<result name="register_success">/page/login.jsp</result>
<result name="register_err">/page/regs.jsp</result>
<!-- 指定Action -->
<result name="login_success" type="redirectAction">
<param name="namespace">/house</param>
<param name="actionName">listHouserAction</param>
</result>
<result name="login_err">/page/login.jsp</result>
<!-- 动态结果 -->
<result name="input">/page/${inputResult}</result>
</action>
</package>
</struts>
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="house" namespace="/house" extends="base">
<action name="*HouserAction" class="com.house.action.HouserAction" method="{1}">
<!-- name dispatcher请求转发到JSP页面 -->
<result>/page/guanli.jsp</result>
</action>
</package>
</struts>