1

我有个问题。A 有一个简单的应用程序。我想使用 Spring MVC 并在 JSP 页面中使用一些 facelets(如果我说得好)。但我无法做到。我正在使用 Geronimo。在 Geronimo 中有 MyFaces JSF 实现。我现在不知道,我该如何写正确faces-config.xml,或者缺少什么。当我在浏览器中打开页面时,Geronimo 会抛出IllegalStateEcxeption没有为此应用程序配置工厂。如果面初始化根本不起作用,就会发生这种情况。

我在应用程序中创建了一个控制器:

@Controller
public class BasicController {
    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

    @ModelAttribute("appVersion")
    public String getVersion() {
        return Version.VERSION + " (" + Version.BUILD_TIME + ")";
    }
}

我已经在 web.xml 中声明了调度程序 servlet 和面对 servlet:

<servlet>
    <servlet-name>sd</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>sd</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<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>*.jsp</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

我在 WEB-INF/sd-servlet.xml 中配置了调度程序 servlet:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="eu.barbucha.trackAnniversaries.webLayer"/>
<mvc:annotation-driven/>
<mvc:resources location="/files/" mapping="/files/**"/>

faces-config.xml只包含一个声明:

<faces-config>
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
</faces-config>

最后我写了一个 JSP 页面:

<?xml version='1.0' encoding='utf-8'?>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="files/basic.css" media="all"/>
</head>
<body>
    <p>Example <h:outputText value="text"/>.</p>
    <hr/>
    <i>${appVersion}</i>
</body>
</html>
4

1 回答 1

0

是的,他们真的可以。但是上面提到的配置有几个错误。

  1. 该文件是WEB-INF/pages/main.jsp,但必须将 JSF servlet 映射到*.jsf并且视图解析器中的后缀必须相同:.jsf. MyFaces 会自动更改后缀。
  2. 然后它还不起作用。你得到IllegalStateException,没有任何应用程序上下文。Spring 的正确配置由两个文件组成。第一个是应用程序上下文,第二个是 Dispatcher servlet 的配置文件。如果您只是创建一个空的应用程序上下文文件,整个 Geronimo 容器可能会冻结。

您应该在两个文件中编写配置。您将它们都放入WEB-INF目录中。首先是applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:annotation-config />
    <context:component-scan base-package="eu.barbucha.trackAnniversaries.webLayer"/>
</beans>

第二个是 servlet 的配置sd,也称为sd-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- The name I've choosen SD = Spring Dispatcher (Servlet) -->
    <context:component-scan base-package="eu.barbucha.trackAnniversaries.webLayer"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsf" />
    </bean>
    <mvc:annotation-driven/>
    <mvc:resources location="/files/" mapping="/files/**"/>
</beans>

使用的解决方案(使用 JSP)与 JSF 1.2 兼容。如果你想用 facelet 代替 JSP,你需要使用 JSF 2.0 兼容的实现,例如 MyFaces 2.0。

于 2011-08-09T12:27:12.537 回答