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>
<s:fielderror />

</div>

2. 在struts.xml中配置配置如下常量时,默认的ui主题会到struts-core的jar包中找对应的配置

<constant value="simple" />

这个配置截图:

Struts中FeildError问题


若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
 *
 *  
 *
 * 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??>
 style="${parameters.cssStyle?html}"<#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 value="simple" />中的value改为自己自定义的值,如

<constant value="mytheme" />

Struts中FeildError问题


然后在src中定义template.mytheme将原来默认的配置全部拷进去,然后在这里面像2那样改掉fielderror.ftl。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/412e2809ef1c466b002efc077fbb88d7.html