我试图让Spring MVC 3和Apache Tiles 2一起玩得很好。我可以显示简单的页面,这些页面是用Tiles模板构建的,但是对于我的生活,我无法调用我的控制器。我将不胜感激任何帮助。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/web-application-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>tiles</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web-application-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Imports the configurations of the different infrastructure systems of the application -->
<import resource="webmvc-context.xml" />
</beans>
webmvc-context.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: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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<!-- Static resource mapping (css, js, images, etc) -->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resouces/" />
<!-- Required stuff for autowiring of controllers -->
<context:annotation-config />
<context:component-scan base-package="com.tarigmaa.gem" />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- Static views (no controller) -->
<!-- <mvc:view-controller path="/index.html" /> -->
<!-- Tiles initialization -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/tiles-defs/templates.xml" />
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />
<!-- Map URLs to controllers -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="mappings">
<props>
<prop key="/index.html">exampleController</prop>
<prop key="/index2.html">exampleController</prop>
</props>
</property>
</bean>
<!-- Define the controllers -->
<bean id="exampleController" class="com.tarigma.GEM.HomeController" />
</beans>
com.tarigma.GEM.HomeController
package com.tarigma.GEM;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome from /index.html");
model.addAttribute ("title", "Nik");
return "index";
}
@RequestMapping(value = "/index2.html", method = RequestMethod.GET)
public String home2(Locale locale, Model model) {
logger.info ("Welcome from /index2.html");
model.addAttribute("title", "Jeff");
return "index2";
}
}
我知道我在这里抛弃了很多代码而且我为此道歉。我已经在这几天了,我的耐心已经不多了。提前谢谢。
答案 0 :(得分:0)
您的RequestMapping应该只是“/ index”或“/ index2”而不是“/index.html”。你的servletmapping“* .html”将确保Spring的DispatcherServlet处理这个调用。