手机版
你好,游客 登录 注册
背景:
阅读新闻

Struts中FeildError问题

[日期:2012-09-02] 来源:Linux社区  作者:chen_jp [字体: ]

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??>
 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 name="struts.ui.theme" value="simple" />中的value改为自己自定义的值,如

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


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

linux
相关资讯       Struts 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款