Spring MVC和MyFaces可以一起工作吗?

时间:2011-08-08 10:17:22

标签: model-view-controller spring myfaces geronimo

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

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

@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只包含一个daclaration:

<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>

1 个答案:

答案 0 :(得分:0)

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

  1. 文件为WEB-INF/pages/main.jsp,但JSF servlet必须映射到*.jsf,并且视图解析器中的后缀必须相同:.jsf。 MyFaces会自动更改后缀。
  2. 然后它还没有用。你得到IllegalStateException,没有任何应用程序上下文。 Spring的正确配置包含两个文件。首先是应用程序上下文和调度程序servlet的第二个配置文件。如果您只是创建一个空的应用程序上下文文件,整个Geronimo容器可能会冻结。
  3. 您应该在两个文件中编写配置。他们都放入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>
    

    第二个是名为sd的servlet配置,也是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。

相关问题