2011-08-08 33 views
1

我有一个问题。 A有一个微不足道的应用程序。我想使用Spring MVC,并在JSP页面中使用一些facelets(如果说我很好)。但我无法做到。我正在使用Geronimo。在Geronimo中有MyFaces JSF实现。我现在没有,我该怎么写适当的faces-config.xml,或缺少什么。当我在浏览器中打开页面时,Geronimo会抛出IllegalStateEcxeption没有为此应用程序配置的工厂。如果脸部初始化完全不起作用,就会发生这种情况。Spring MVC和MyFaces可以一起工作吗?

我已经在应用程序中创建的复合控制器:

@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 + ")"; 
    } 
} 

我已经宣布调度servlet和在web.xml面临的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配置调度的servlet /sd-servlet.xml:

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

回答

0

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

  1. 该文件是WEB-INF/pages/main.jsp,但JSF servlet必须被映射到*.jsf并在视图解析器后缀必须是相同的:.jsf。 MyFaces会自动更改后缀。
  2. 然后它还没有工作。你得到IllegalStateException,没有任何应用程序上下文。 Spring的正确配置由两个文件组成。首先是调度程序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。

相关问题