struts2验证失败后返回input,select中list数据丢失解决方法

时间:2021-09-07 15:01:17

 解决思路:自己建立el标签,通过静态方法。

 代码如下:

   1:           data.tld

 <?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    <tlib-version>1.0</tlib-version>
    <short-name>myf</short-name>
      <function>
        <description>getAppList</description>
        <name>getAppList</name>
        <!-- 调用EL方法的名称 -->
        <function-class>com.tongtech.em.view.application.storage.DataUtils</function-class>
        <function-signature>
            java.lang.String getAppList(java.lang.String,java.lang.String,java.lang.String)
        </function-signature>
        <example>${myf:getAppList(1,2)}</example>
        <!-- 例如 -->
    </function>
           </taglib>

2:

类 DataUtils

 

package com.tongtech.em.view.application.storage;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.tongtech.em.master.storage.AppInfo;
import com.tongtech.em.master.storage.ManagerImpl;
import com.tongtech.em.master.storage.StorageAreaInfo;
import com.tongtech.em.master.usermanager.AuthenticationService;
import com.tongtech.em.persistence.usermanager.dataobject.User;

public final class DataUtils {


public static String getAppList(String appId, String areaId, String storageId) {
 ManagerImpl applicationService = new ManagerImpl();
 List<AppInfo> list = new ArrayList<AppInfo>();
 list = applicationService.getAppInfos(areaId, storageId);
 StringBuffer temp = new StringBuffer("<select id =\"existNameSelect\" name=\"appNameExist\" theme=\"simple\" onchange=\"changeData(this)\">");
 if (StringUtils.isEmpty(appId)) {
  temp.append("<option value=\"\" selected>请选择</option>");
 } else {
  temp.append("<option value=\"\">请选择</option>");
 }
 for (AppInfo appInfo : list) {
  if (appInfo.getAppId().equals(appId)) {
   temp.append("<option value=\"" + appInfo.getAppId() + "\" selected>" + appInfo.getAppName() + "</option>");
  } else {
   temp.append("<option value=\"" + appInfo.getAppId() + "\" >" + appInfo.getAppName() + "</option>");
  }
 }
    temp.append("</select>");
      return temp.toString();
}

}

3:页面显示:<%@taglib uri="/WEB-INF/tlds/data.tld" prefix="myf"%>

   调用: ${myf:getAppList(appInfo.appId,appInfo.areaId,appInfo.storageId)}