STRUTS2配置动态页面

CreateTime--2017年5月11日09:00:31
Author:Marydon

  1.struts配置 

<?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>
<!-- struts2配置 -->

<!-- struts2常用配置 START -->
<!--指定web应用的默认编码集,相当于调用HttpServletRequest.setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定需要struts2处理的请求后缀,默认值为action. 如果用户需要指定多个请求后缀,则多个后缀之间以英语逗号(,)隔开 -->
<constant name="struts.action.extension" value="do" />

<!--当struts.xml文件改变后,系统是否自动的重新加载该文件,默认值为false -->
<constant name="struts.configuration.xml.reload" value="true" />

<!--
是否应用开发模式. 通常,应在开发阶段设置为true,出错时显示更多、更友好的出错提示; 当进入产品发布阶段后,应该设置为false
-->
<constant name="struts.devMode" value="true" />
<!--当找不到url时是否根据namespace向上级路径查询,默认值为false -->
<constant name="struts.mapper.alwaysSelectFullNamespace" value="true"></constant>
<!--配置上传文件大小 -->
<constant name="struts.multipart.maxSize" value="10000000" />
<constant name="struts.multipart.saveDir " value="" />
<!--
该属性指定Struts 2框架默认加载的配置文件,如果需要指定默认加载多个配置文件, 则多个配置文件的文件名之间以英文逗号(,)隔开。
该属性的默认值为struts-default.xml,struts-plugin.xml,struts.xml
-->
<constant name="struts.configuration.files" value="struts-default.xml" />
<!-- END struts2常用配置 -->

<!-- 配置动态页面 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

<package name="PATIENT" extends="struts-platform" namespace="/telemedicine/patient">
<!-- 查询
已申请的*、已安排的*、未通过的*、已撤销的*、*审核、*报告、未完成的*、已完成的*、*效果评价
所对应的列表页面
-->
<action name="index" class="telemedicine.web.actions.reseCons.PATIENT_Action" method="index">
<!-- 动态页面 -->
<result name="success" type="dispatcher">${dispatcherPath1}</result>
</action>
</package>
</struts>

  2.Action类配置

//首页页面转发路径
private String dispatcherPath1;
//生成对应的get、set方法
public String getDispatcherPath1() {
return dispatcherPath1;
}

public void setDispatcherPath1(String dispatcherPath1) {
this.dispatcherPath1 = dispatcherPath1;
}

// 转发路径
String dPath = "";
// 状态:根据不同的状态值设置不同的页面路径
// 自定义状态(用于区分跳转页面) 1:已申请的*;2:已撤销的*;3:*审核;4:未通过的*;
// 5:已安排的*;6:未完成的*;7:*报告;8:已完成的*;9:*效果评价(9 未定)
String modelAndView = "";

/**
* *审核信息表
* @return 导航到首页
*/
public String index() {
  try {
  // 必要参数:跳转页面-modelAndView
  paramsMap = WebUtils.getParameterMap();
  // modelAndView为必要参数
  if (paramsMap.containsKey("modelAndView")) {
    // 自定义状态(用于区分跳转页面) 1:已申请的*;2:已撤销的*;3:*审核;4:未通过的*;
     // 5:已安排的*;6:未完成的*;7:*报告;8:已完成的*;9:*效果评价(9 未定)
    modelAndView = paramsMap.get("modelAndView").toString().trim();

    if ("1".equals(modelAndView))
    dPath = "/telemedicine/remoteRese/consAlready/consAleady_records.jsp";// 已申请的*
    else if ("2".equals(modelAndView))
     dPath = "/telemedicine/remoteRese/consRevoked/consRevoked_records.jsp";// 已撤销的*
    else if ("3".equals(modelAndView))
     dPath = "/telemedicine/remoteCons/consReview/consReview_records.jsp";// *审核
    else if ("4".equals(modelAndView))
     dPath = "/telemedicine/remoteRese/consNopass/consNopass_records.jsp";// 未通过的*
     else if ("5".equals(modelAndView))
      dPath = "/telemedicine/remoteRese/consPlannad/consPlannad_records.jsp";// 已安排的*
     else if ("6".equals(modelAndView))
     dPath = "/telemedicine/remoteCons/consUncomp/consUncomp_records.jsp";// 未完成的*
     else if ("7".equals(modelAndView))
     dPath = "/telemedicine/remoteCons/consReport/consReport_records.jsp";// *报告
     else if ("8".equals(modelAndView))
      dPath = "/telemedicine/remoteCons/consComp/consComp_records.jsp";// 已完成的*
      else if ("9".equals(modelAndView))
      dPath = "/telemedicine/remoteCons/consEval/consEval_records.jsp";// *效果评价

     // 设置跳转页面路径
     this.setDispatcherPath1(dPath);
  }
} catch (Exception e) {
  this.code = -1;
  this.msg = e.getMessage();
  this.expMsg = getExceptionMessage(e);
  log.error(e.getMessage());
  }
  return getResult();
}

  要点解析:

1.struts转发路径用"${动态值}"代替死值;

2.在Action类中生成动态值的get(),set()方法。

 

 

作者:​​Marydon