Spring MVC + JSF:表单URL错误

时间:2013-07-29 07:34:54

标签: forms jsf java-ee spring-mvc

我尝试将Spring MVC与JSF一起使用。但我使用JSF作为表示层。显示我的@Controller的JSF视图不是问题...但是如果我使用h:form,则渲染器使用错误的URL(没有我的调度程序)。

这是获取视图的正确URL(method = GET):
http://localhost:8080/AppName/dispatcher/testController/create

使用此URL呈现JSF视图表单:
http://localhost:8080/AppName/WEB-INF/jsf/testController/create

表单的正确URL是(method = POST):
http://localhost:8080/AppName/dispatcher/testController/create

我的控制器:

@Controller
@RequestMapping(value = "/testController")
public final class TestController
{

  [...]

  @RequestMapping(value = "/create", method = RequestMethod.GET)
  public void getCreateNew(final Model model)
  {
    [...]
  }

  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public void postCreateNew(final Model model)
  {
    [...]
  }
}

我的观点:

<?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"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:l="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
     >

    <h:head>
        <title>Create new</title>
    </h:head>
    <h:body>
        <h:messages errorClass="error"/> 

        <f:view>
            <h:form>
                <h:panelGrid columns="2" columnClasses="label, value" styleClass="grid">
                    <h:outputText value="${name}" />
                    <p:inputText label="Test: ${name}" required="true">
                        <f:validateLength minimum="3" />
                    </p:inputText>
                </h:panelGrid>
                <p:commandButton type="submit" value="${labelSubmit}" action="spring:@testController.doneCreateNew" />
            </h:form>
        </f:view>
    </h:body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/dispatcher/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>

faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.1"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">

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

</faces-config>

dispatcher-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

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

</beans>

和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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven />
    <context:annotation-config/>
    <context:component-scan base-package="com.sommer_engineering"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

</beans>

有人知道为什么这不起作用?我会很高兴任何想法...

2 个答案:

答案 0 :(得分:1)

请参阅下面的编辑

我会在这里留下我的答案,因为我必须自己解决同样的问题。通过简单的漂亮配置集成重写规则对我有用。

  1. 添加PrettyFaces的依赖项 (注意:如果你不熟悉PrettyFaces,那么它可能不是你认为的那样)

  2. 添加重写规则

  3. 如果调度程序的servlet映射如下:

        <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>/dispatcher/*</url-pattern>
        </servlet-mapping>
    

    并且您的视图解析器具有以下属性:

        p:prefix="/WEB-INF/jsf/" p:suffix=".jsf"
    

    那么你的pretty-config.xml应该是这样的:

         <pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
                      http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">
    
            <url-mapping id="pretty_spring_urls">
                <pattern value="/dispatcher/WEB-INF/jsf/{spring}.jsf" />
                <view-id value="/dispatcher/{spring}" />
            </url-mapping>
    
        </pretty-config>
    

    修改 然而,Prettyfaces仅在嵌入式glassfish服务器上为我工作。 Urlrewrite曾在glassfish,tomcat 7和jboss上为我工作过。

答案 1 :(得分:0)

您可以尝试将其暗示为dispatcher-servlet.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="" p:suffix=".jsf" />