Java 使用<;显示jsf片段中的错误;t:消息>;标签
我只是想知道是否可以在单独的jsp片段上显示错误 我有一个要求,我必须显示超链接错误(即,单击任何错误都会聚焦到相应的输入字段) 例如,如果我有一个表单,有20个字段,那么我假设使用20倍标签。我知道我可以对所有错误使用单个标记,但我有一个关注单个字段的要求,我不确定我们如何使用它来实现这一点。然而,来自标记的onclick事件解决了我的问题,但使用20个标记在标题下留下了很大的空间,页面看起来不太好看 所以我在想,是否可以将所有错误标记放在单独的jsf片段上,然后在主页上进行jsp包含 我尝试了,但没有显示错误消息Java 使用<;显示jsf片段中的错误;t:消息>;标签,java,jsf,Java,Jsf,我只是想知道是否可以在单独的jsp片段上显示错误 我有一个要求,我必须显示超链接错误(即,单击任何错误都会聚焦到相应的输入字段) 例如,如果我有一个表单,有20个字段,那么我假设使用20倍标签。我知道我可以对所有错误使用单个标记,但我有一个关注单个字段的要求,我不确定我们如何使用它来实现这一点。然而,来自标记的onclick事件解决了我的问题,但使用20个标记在标题下留下了很大的空间,页面看起来不太好看 所以我在想,是否可以将所有错误标记放在单独的jsf片段上,然后在主页上进行jsp包含 我尝试
有没有人知道,我该如何做到这一点呢?我认为t:信息是最重要的
也许一个定制的JSF控件可以满足这一需求——有时您无法操纵现有的标记来获得您想要的确切输出 您可以从中获取消息。控件的呈现程序将HTML写入页面:
package custom; /*imports omitted*/
public class CustomErrorRenderer extends Renderer {
@Override @SuppressWarnings("unchecked")
public void encodeEnd(FacesContext context,
UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("div", component);
writer.writeAttribute("id", component.getClientId(context), "id");
writer.writeAttribute("style", "color: red", null);
writer.startElement("ul", null);
Iterator<String> clientIds = context.getClientIdsWithMessages();
while (clientIds.hasNext()) {
String clientId = clientIds.next();
Iterator<FacesMessage> messages = context.getMessages(clientId);
if (!messages.hasNext()) { continue; }
String javaScript = "var field = document.getElementById('"
+ clientId + "');" + "if(field == null) return false;"
+ "field.focus(); return false;";
writer.startElement("li", null);
writer.startElement("a", null);
writer.writeAttribute("onclick", javaScript, null);
writer.writeAttribute("href", "#", null);
while (messages.hasNext()) {
writer.writeText(messages.next().getSummary(), null);
}
writer.endElement("a");
writer.endElement("li");
}
writer.endElement("ul");
writer.endElement("div");
}
}
这在TLD文件中定义:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>custom</short-name>
<uri>http://custom</uri>
<tag>
<name>errors</name>
<tag-class>custom.CustomErrorTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
1
习俗
http://custom
错误
custom.CustomErrorTag
空的
如果您希望通过单独的页面包含此内容,此示例JSP将控制:
<%@ taglib prefix="custom" uri="http://custom"%>
<custom:errors />
此JSP包括:
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%><%@taglib
uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<body>
<f:view>
<h:form>
<h:inputText>
<f:validateLength minimum="6" maximum="10" />
</h:inputText>
<h:inputText>
<f:validateLength minimum="4" maximum="6" />
</h:inputText>
<h:commandButton />
</h:form>
<f:subview id="errs">
<jsp:include page="customerrors2_inc.jsp" />
</f:subview>
</f:view>
</body>
</html>
是的,这看起来太复杂了,但至少您可以使用它创建一个JAR,其中包含一个可重用的自定义控件,该控件可以放入任何JSF应用程序中
使用托管bean和重复控件可能会获得类似的结果,但随后您必须开始担心将列表创建延迟到验证阶段之后(这可能涉及阶段侦听器)。谢谢您的时间。这段代码非常有效。但是如果我在我的更新表单中添加了一些验证,然后单击我的更新方法,然后会显示超链接错误(这是我想要的),但同时它会清除更新表单中的所有内容。例如,如果有20个更新输入字段,则会清除所有字段。但是如果我用替换,那么它会给出类似的错误,但是表单不会被清除。你能修一下这个吗。谢谢
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>custom</short-name>
<uri>http://custom</uri>
<tag>
<name>errors</name>
<tag-class>custom.CustomErrorTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
<%@ taglib prefix="custom" uri="http://custom"%>
<custom:errors />
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%><%@taglib
uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<body>
<f:view>
<h:form>
<h:inputText>
<f:validateLength minimum="6" maximum="10" />
</h:inputText>
<h:inputText>
<f:validateLength minimum="4" maximum="6" />
</h:inputText>
<h:commandButton />
</h:form>
<f:subview id="errs">
<jsp:include page="customerrors2_inc.jsp" />
</f:subview>
</f:view>
</body>
</html>