我有一个Java EE应用程序,它有JSF2 + PrettyFaces + Facelets + EJB3 + EclipseLink。
由于JSF2 + Faceletes呈现模型中的一些不一致与PrettyFaces和h:link标签一起使用,我不断遇到如上所述的IllegalStateException。
根据使用JSF2 + Faceletes时的良好实践,我删除了所有旧的JSTL标记以及所有的commandLink标记。
在web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<display-name>atlPortal</display-name>
<context-param>
<param-name>com.sun.faces.prefer.XHTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/facelets/customTags.taglib.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.mindvortex.atl.web.common.servlet.ImageServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image/*</url-pattern>
<url-pattern>/pages/image/*</url-pattern>
<url-pattern>/pages/protected/image/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
<async-supported>false</async-supported>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<!-- servlets and such would be above -->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>userauth</realm-name>
<form-login-config>
<form-login-page>/login.faces</form-login-page>
<form-error-page>/loginError.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<display-name>Block All XHTML</display-name>
<web-resource-collection>
<web-resource-name>blockXHTML</web-resource-name>
<description></description>
<url-pattern>*.xhtml</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>HEAD</http-method>
<http-method>PUT</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
<security-constraint>
<display-name>ConstraintSSL</display-name>
<web-resource-collection>
<web-resource-name>protected</web-resource-name>
<description/>
<url-pattern>/pages/protected/*</url-pattern>
<url-pattern>/login/*</url-pattern>
<url-pattern>/login.*</url-pattern>
<url-pattern>/account/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>HEAD</http-method>
<http-method>PUT</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<user-data-constraint>
<description>SSL not required for Development</description>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>ConstraintUser</display-name>
<web-resource-collection>
<web-resource-name>user</web-resource-name>
<description/>
<url-pattern>/account/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>HEAD</http-method>
<http-method>PUT</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>ADMINISTRATORS</role-name>
<role-name>USERS</role-name>
</auth-constraint>
<user-data-constraint>
<description>SSL not required for Development</description>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-role>
<description/>
<role-name>USERS</role-name>
</security-role>
<security-role>
<description/>
<role-name>ADMINISTRATORS</role-name>
</security-role>
<session-config>
<session-timeout>30</session-timeout>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<welcome-file-list>
<welcome-file>init.faces</welcome-file>
</welcome-file-list>
</web-app>
每当我从一个XHTML页面导航到另一个XHTML页面时,就会发生这种情况
<h:link outcome="pretty:viewContactUs" styleClass="nav6">
#{msg['contactUs']}
</h:link>
在pretty-faces.xml
中 <url-mapping id="viewContactUs">
<pattern value="/contactUs/" />
<view-id value="contactUs.faces" />
<action>#{commentsMB.openContactUs}</action>
</url-mapping>
@ManagedBean
@RequestScoped
public class CommentsMB extends UserCrudMB<Comments, Integer> {
private static final long serialVersionUID = 1L;
@EJB(mappedName="ejb/CommentsService")
private CommentsServiceBeanLocal commentsService;
// ... Code
public String openContactUs() {
this.entity = new Comments();
return NavigationViews.VIEW_CONTACT_US;
}
}
在faces-config.xml中:
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>contactUs</from-outcome>
<to-view-id>/pages/contactUs.xhtml</to-view-id>
<redirect /></navigation-case>
在contactUs.xhtml中:
<div class="form_row">
<h:link id="commentsSave" outcome="pretty:commentsSave" styleClass="contact">
#{msg.send}
</h:link>
</div>
单击commentsSave按钮时会发生错误。它给出:IllegalStateException:PWC3990:已经为此响应调用了getWriter()。
commentsSave URL映射的pretty-config.xml:
<url-mapping id="commentsSave">
<pattern value="/comments/save/" />
<view-id value="contactUs.faces" />
<action>#{commentsMB.save}</action>
</url-mapping>
评论MB保存动作签名:
public String save() {
return save(this.getEntity());
}
没有调用注释MB.save Bean操作,并且在日志中抛出异常时出现页面未找到错误。
堆栈跟踪:
[#|2012-03-30T13:47:54.968-0300|SEVERE|glassfish3.1.1|org.apache.jasper.servlet.JspServlet|_ThreadID=25;_ThreadName=http-thread-pool-8084(4);|PWC6117:
File "C%3A%5CGBL%5Cprop%5Cprogs%5Cglassfish-3.1.1%5Cglassfish3%5Cglassfish%5Cdomains%5Cdomain1%5Capplications%5CatlanteusEAR%5CatlanteusPortal-1.0.0_
war%5Ccomments%5Csave%5CcontactUs.jsp" not found|#]
[#|2012-03-30T13:47:54.976-0300|WARNING|glassfish3.1.1|org.apache.catalina.core.ApplicationDispatcherForward|_ThreadID=25;_ThreadName=http-thread-pool
-8084(4);|Exception processing ErrorPage[errorCode=404, location=/pages/error/error404.xhtml]
java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:370)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
at org.apache.catalina.core.ApplicationDispatcherForward.custom(ApplicationDispatcherForward.java:253)
at org.apache.catalina.core.ApplicationDispatcherForward.status(ApplicationDispatcherForward.java:209)
at org.apache.catalina.core.ApplicationDispatcherForward.commit(ApplicationDispatcherForward.java:131)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:353)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:110)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:619)
|#]
似乎JSF将导航解释为.jsp,尽管我只使用XHTML。
有人可以帮我弄清楚我做错了什么吗?自从我开始使用JSF2 + Facelets以来,这是我的主要问题。
答案 0 :(得分:1)
你的“contactUs.faces”view-id值前面不需要斜杠'/'吗?
我认为应该是:
“/ contactUs.faces”,对吧?
〜林肯
答案 1 :(得分:1)
在考虑答案后,我提出了最终的解决方案,所以我会在这里发帖,让其他人明白:
更改了pretty-faces.xml中的URL映射,以使所有JSF资源完全合格: 例如,如果contactUs.xhtml位于/ pages /下,则映射将为:
<url-mapping id="viewContactUs">
<pattern value="/contactUs/" />
<view-id value="/pages/contactUs.faces" />
<action>#{commentsMB.openContactUs}</action></url-mapping>
从faces-config.xml中删除了所有JSF导航规则。例如:
之类的规则<navigation-case>
<from-outcome>contactUs</from-outcome>
<to-view-id>/pages/contactUs.xhtml</to-view-id>
</navigation-case>
将被删除或注释掉。执行此操作后,实际的导航映射将仅存在于pretty-config.xml中,并且可以与PrettyFaces过滤器一起正常工作。