我正在尝试使用在Tomcat本地服务器上运行的maven来创建一个简单的Spring Web MVC项目 . 调度程序servlet xml是:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SpringMVC14</display-name>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<!-- - Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- - Servlet that dispatches request to registered Controller -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的viewResolver的mvc-config.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- base-package here: -->
<context:component-scan base-package="com.sat.controllers"/>
<mvc:annotation-driven />
<!-- View Resolver for jsp pages -->
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> -->
<!-- THYMELEAF: Template Resolver for webapp pages -->
<bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/" />
<property name="templateMode" value="*.html,*.xhtml,*jsp" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="2" />
<property name="cacheable" value="true" />
</bean>
<bean id="webTemplateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="webTemplateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="webTemplateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html,*.xhtml, *.jsp" />
</bean>
</beans>
控制器是:
@Controller
public class helloController {
@RequestMapping(value = "/hello")
public ModelAndView helloWorld(Model model) {
ModelAndView mav = new ModelAndView();
mav.setViewName("showMessage");
String msg = "hello from message controller";
mav.addObject("myVar", msg);
return mav;
}
@RequestMapping(value = "/thyme")
public String thyme(@RequestParam(value="myName", required= false, defaultValue="Satish") String myName, Model model) {
model.addAttribute("myAttr", myName);
return "/templates/thyme";
}
}
我的主要课程是:
public class App {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx= new ClassPathXmlApplicationContext("application-config.xml");
}
}
welcome.jsp页面是:
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Hello from index jsp</h2>
<p>
<a href="hello">Click for hello</a>
<p>
<a href="thyme">Click for thyme</a>
</body>
</html>
和我的百里香HTML页面是:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
Hello from thyme HTML page
</body>
</html>
如果我使用InternalResourceViewResolver和jsp,应用程序运行,但我想使用Thymeleaf而不是jsp .
我的HTML百万美元文件位于资源\ WEB-IN \ templates中 . jsp页面位于resources \ WEB-IN \ views文件夹中 . 我在哪里做错了?请建议......它在github上http://tinyurl.com/nrq9pm5