Fro*_*ost 6 jsf facelets hyperlink
我正在使用JSF 2.1,我遇到了<h:link>标签的一些问题.我正在尝试将链接的结果点从我的XHTML文件转换为纯HTML文件.但是,当我运行我的Web应用程序时,.html链接生成的URL中的.xhtml扩展名会自动转换为扩展名.
这是我的Facelet文件:
<h:body>
<h:form>
<h:link value="animation" outcome="#{contentForm.ccAnimationLink}"/>
</h:form>
<h:body>
Run Code Online (Sandbox Code Playgroud)
这是我的contentForm bean:
package my.app.content;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class ContentForm implements Serializable {
private static final long serialVersionUID = -8463354828156798513L;
private String ccAnimationLink = "";
@PostConstruct
public void init() {
ccAnimationLink = "/content/cc/CC_animation/story.html";
}
public String getCcAnimationLink() {
return ccAnimationLink;
}
public void setCcAnimationLink(String ccAnimationLink) {
this.ccAnimationLink = ccAnimationLink;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行此应用程序时,我收到以下错误:
无法从视图ID'/content/modifiedcc.xhtml'中找到匹配的导航案例,以获得结果'/content/cc/CC_animation/story.html'
我确保我的URL正确,所以我story.xhtml也在该位置创建了一个文件.使用该文件,它运行没有错误.
如果我拉动生成页面的源代码,我可以看到它<h:link>已正确更改为"a href"标记,如下所示:
<a href="/MyWebApp/content/cc/CC_animation/story.xhtml">animation</a>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,虽然URL改变了story.html,就像我在我的bean中一样,改为story.xhtml.
如何确保它保持为story.html而不是将其更改为xhtml?
这是我的web.xml文件:
<display-name>MyWebApp</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>jsf/login/login.xhtml</welcome-file>
</welcome-file-list>
<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-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我尝试添加以下内容web.xml,这没有什么区别:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我确信这有一个非常简单的解决方案,我现在只是想念它.帮助将受到高度赞赏.我希望这个问题很清楚.
JSF 2.0+引入了一个<h:link>组件,该组件在其outcome属性中获取导航案例结果,并自行生成正确的URL.这就是为什么你没有为faces servlet添加另一个映射而出现错误的原因:只需重新读取错误.它对于创建JSF间应用程序导航非常有用.
或者,总有一个<h:outputLink>组件在其value属性中采用URL .它对于创建JSF应用程序外部的导航很有用.
请务必阅读BalusC关于该问题的经典:何时应该使用h:outputLink而不是h:commandLink?.
也就是说,考虑以下组件:
<h:link outcome="/destination" value="link" />
<h:outputLink value="/destination.html">
outputLink
</h:outputLink>
Run Code Online (Sandbox Code Playgroud)
前一个组件将通过附加一个faces servlet映射将其结果转换为一个实际的URL,而后一个组件将保持其原样值.
因此,使用以下组件可以解决您的问题:
<h:outputLink value="#{contentForm.ccAnimationLink}">
Animation
</h:outputLink>
Run Code Online (Sandbox Code Playgroud)