0

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

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

JSF 视图表单使用以下 URL 呈现:
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>

调度程序-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>

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

4

2 回答 2

1

请参阅下面的编辑

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

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

  2. 添加重写规则

如果调度程序的 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 上为我工作过。

于 2013-09-07T15:57:11.467 回答
0

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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="" p:suffix=".jsf" />
于 2013-07-29T07:57:56.257 回答