为什么我的 JSF + Spring Web 应用程序输出 JSF 源代码而不是解释的 HTML 页面? [英] Why does my JSF + Spring web application output JSF source code instead of interpreted HTML page?

查看:13
本文介绍了为什么我的 JSF + Spring Web 应用程序输出 JSF 源代码而不是解释的 HTML 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JSF 和 Spring Framework 的新手,我正在尝试弄清楚如何让它们协同工作.我当前的问题是应用程序输出我的 JSF 文件而不解释它们.以下是我认为可能相关的一些代码片段:

I'm new to both JSF and Spring Framework and I'm trying to figure out how to make them work together. My current problem is that application outputs my JSF files without interpreting them. Here are some snippets of my code which I believe might be relevant:

dispatcher-servlet.xml

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="login.htm">loginController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/pages/"
      p:suffix=".xhtml" />

<bean name="loginController" class="controller.LoginController" />

登录控制器

public class LoginController extends MultiActionController {
 public ModelAndView login(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
    System.out.println("LOGIN");
    return new ModelAndView("login");
}

WEB-INF/pages/login.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>#{message.log}</title>
</h:head>
<h:body>
    <h:form>
        <h:outputLabel value="#{message.username}" for="userName">
            <h:inputText id="userName" value="#{User.name}" />
        </h:outputLabel>            
        <h:commandButton value="#{message.loggin}" action="#{User.login}" />
    </h:form>
</h:body>
</html>

有什么想法可能出问题吗?这段代码有任何意义吗?我很清楚事实,这可能很糟糕,我很高兴在这里说明为什么它很糟糕以及如何使它变得更好.谢谢:)

Any ideas where the problem might be? Does this code make any sense at all? I'm well aware of fact, that probably completely sucks and I'll be glad to here WHY it sucks and how to make it better. Thanks :)

我正在添加一段代码,这似乎是问题的根源,我(当然)没有包含在原始问题中:

I'm adding piece of code which seems to be the root of the problem and which I (of course) didn't include in the original question:

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<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>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

Faces Servlet 的 url-pattern 必须更改为 *.xhtml 才能正常工作.

The url-pattern of Faces Servlet had to be changed to *.xhtml in order to work properly.

推荐答案

如果 JSF 标签没有被解析,那么它只是意味着请求 URL 不匹配 url-patternFacesServletweb.xml 中定义.它负责所有 JSF 发生的事情.您需要验证请求 URL 是否与 FacesServleturl-pattern 匹配.如果它是例如 *.jsf,那么您需要通过 http://example.com/context/page.jsf 而不是通过 调用它http://example.com/context/page.xhtml.

If the JSF tags are not been parsed, then it simply means that the request URL did not match the url-pattern of the FacesServlet as definied in web.xml. It is namely responsible for all that JSF happenings. You need to verify if the request URL matches the url-pattern of the FacesServlet. If it is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf and thus not by http://example.com/context/page.xhtml.

然而我不确定 Spring 是如何适应图片的,因为我不使用它,但是为了让 JSF 工作,你真的需要首先通过 FacesServlet 传递请求,而不是通过一些弹簧控制器.Spring 应该在之后完成它的工作.

I am however not sure how Spring fits in the picture since I don't use it, but to get JSF to work you really need to pass the request through the FacesServlet first, not through some Spring controller. Spring should do its job thereafter.

这篇关于为什么我的 JSF + Spring Web 应用程序输出 JSF 源代码而不是解释的 HTML 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆