歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Struts中FeildError問題

Struts中在前台中通過<s: fielderror />拿到action中addFieldError("fielderror.test", "wrong!")添加的錯誤信息是,默認情況下是帶了<ul><li>...</li></ul>格式的。這個給自己設定錯誤信息的表現帶來不便,三種解決方法:

1. 在前台jsp頁面中加入css控制

 .formFieldError ul li{

list-style-type: none
}  

<div class="formFieldError">
<s:fielderror />

</div>

2. 在struts.xml中配置配置如下常量時,默認的ui主題會到struts-core的jar包中找對應的配置

<constant name="struts.ui.theme" value="simple" />

這個配置截圖:


若value=simple,則找template.simple中配置,這裡的value還可以取值xhtml,css_xhtml,xhtml,那麼對應的就找對應的template中的ui配置,其中有一個文件為/template/simple/fielderror.ftl。只要自己在src下定義一個相同名稱的配置,然後刪除其中的加格式的部分就可以了。


刪除後如下,直接拷貝到fielderror.ftl中

<#--
/*
 * $Id: fielderror.ftl 722375 2008-12-02 05:19:57Z wesw $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<#if fieldErrors??><#t/>
<#assign eKeys = fieldErrors.keySet()><#t/>
<#assign eKeysSize = eKeys.size()><#t/>
<#assign doneStartUlTag=false><#t/>
<#assign doneEndUlTag=false><#t/>
<#assign haveMatchedErrorField=false><#t/>
<#if (fieldErrorFieldNames?size > 0) ><#t/>
<#list fieldErrorFieldNames as fieldErrorFieldName><#t/>
<#list eKeys as eKey><#t/>
<#if (eKey = fieldErrorFieldName)><#t/>
<#assign haveMatchedErrorField=true><#t/>
<#assign eValue = fieldErrors[fieldErrorFieldName]><#t/>
<#if (haveMatchedErrorField && (!doneStartUlTag))><#t/>
<ul<#rt/>
<#if parameters.cssClass??>
 class="${parameters.cssClass?html}"<#rt/>
<#else>
 class="errorMessage"<#rt/>
</#if>
<#if parameters.cssStyle??>
 <#rt/>
</#if>
>
<#assign doneStartUlTag=true><#t/>
</#if><#t/>
<#list eValue as eEachValue><#t/>
<li><span>${eEachValue}</span></li>
</#list><#t/>
</#if><#t/>
</#list><#t/>
</#list><#t/>
<#if (haveMatchedErrorField && (!doneEndUlTag))><#t/>
</ul>
<#assign doneEndUlTag=true><#t/>
</#if><#t/>
<#else><#t/>
<#if (eKeysSize > 0)><#t/>

<#list eKeys as eKey><#t/>
<#assign eValue = fieldErrors[eKey]><#t/>
<#list eValue as eEachValue><#t/>
<span>${eEachValue}</span>
</#list><#t/>
</#list><#t/>

</#if><#t/>
</#if><#t/>
</#if><#t/>

3. 直接將struts.xml中的<constant name="struts.ui.theme" value="simple" />中的value改為自己自定義的值,如

<constant name="struts.ui.theme" value="mytheme" />


然後在src中定義template.mytheme將原來默認的配置全部拷進去,然後在這裡面像2那樣改掉fielderror.ftl。

Copyright © Linux教程網 All Rights Reserved