我正在开发基于 Spring 和 ICEFaces 的软件。
我有一个文件<project directory>/src/main/webapp/WEB-INF/views/<filename>.xhtml
,可以使用以下 URL 正确访问:http://<hostname>/<projectname>/<filename>.xhtml
该文件包含一个<h:form id="formId">
呈现为<form action="/<projectname>/WEB-INF/views/<filename>.xhtml" [.. some other stuff ..]>
这意味着,当我单击表单中包含的输入提交时,浏览器会尝试打开 URL http://<hostname>/<projectname>/WEB-INF/views/<filename>.xhtml
,正如我在标题中所说,它会显示错误 404 页面。
我希望文件 .xhtml 也可以使用“更长”的 URL 来访问。我很确定由于配置错误,我目前无法实现这一目标。
这是我的web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_2_5.xsd"
version="2.5">
<web-app>
<display-name>SIGLO</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是上一个文件中引用 的applicationContext.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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="http://www.springframework.org/schema/beans"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >
<context:component-scan base-package="com.infoone.siglo" />
<!--
map all requests to /resources/** to the container default servlet
(ie, don't let Spring handle them)
-->
<bean id="defaultServletHttpRequestHandler" class="org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler" />
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="urlMap" >
<map>
<entry key="/resources/**" value-ref="defaultServletHttpRequestHandler" />
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<mvc:annotation-driven />
<!-- JSF for representation layer. All JSF files under /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
<property name="cache" value="false" />
<property name="viewClass" value="org.springframework.faces.mvc.JsfView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean name="icefacesResourceHandler" class="org.springframework.faces.webflow.JsfResourceRequestHandler" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="0" />
<property name="mappings">
<value>
/javax.faces.resource/**=icefacesResourceHandler
</value>
</property>
</bean>
</beans>
最后,这是我的faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.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-facesconfig_2_0.xsd">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<locale-config></locale-config>
<resource-bundle>
<base-name>MessageResources</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
让我指出,这个配置仍然不允许我成功打开较短的URL。事实上,我必须创建一个合适的控制器,或者更好的是,在@Controller中创建一个合适的@RequestMapping:
@RequestMapping(value = "<filename>", method = RequestMethod.GET)
public String creaBlocco()
{
return "<filename>";
}
@RequestMapping(value = "<filename>", method = RequestMethod.POST)
public String creaBlocco([.. parameters ..]) {
[.. stuff ..]
return "<filename>";
}
是的,@RequestMapping的值是"<filename>"
,没有.xhtml
扩展名。通过反复试验,我已经确定这种映射对于 GET 成功是必要的。另一方面,我意识到这样的配置真的很脆弱。我应该在我的配置文件中进行哪些更改,以便使用更长的URL<filename>.xhtml
也可以访问?
提前感谢您的关注。