小弟最近学习搭建Spring MVC框架的web应用,工具是Myeclipse10,在配置velocity的视图解析器时遇见1奇怪问题,烦请高手解答。
问题简述:项目搭建完毕启动访问url后,最初是报 找不到layout.vm这个文件而出错,在项目里面新建该文件后,后台无报错了,但是无论Action返回视图a或b,最终都会显示layout里面的内容,从而达不到页面跳转的目的。
下面附上几张项目配置的图片供参考。
项目文件结构:
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">
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springConfig.xml</param-value>
</context-param>
<!-- POST中文乱码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springServlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 项目首页 -->
<welcome-file-list>
<welcome-file>hello.do</welcome-file>
</welcome-file-list>
</web-app>
springServlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 扫描注解Bean -->
<context:component-scan base-package="com.myWeb.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 注入 HandlerMapping与HandlerAdapter -->
<mvc:annotation-driven />
<!-- 静态资源映射 -->
<mvc:default-servlet-handler />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<!-- 视图解析器 -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> -->
<!-- <property name="contentType" value="text/html" /> -->
<!-- <property name="prefix" value="/WEB-INF/pages/" /> -->
<!-- <property name="suffix" value=".jsp" /> -->
<!-- </bean> -->
<!-- velocity视图解析 -->
<bean class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<!-- 经测试,下面这项配置无效,只有properties文件中的设置生效 -->
<!-- <property name="resourceLoaderPath" value="/WEB-INF/pages/" /> -->
<property name="configLocation" value="classpath:velocity.properties" />
</bean>
<bean class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="cache" value="false" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/classes/velocityTools.xml" />
</bean>
<!-- 文件上传相关 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="10485760" />
</bean>
</beans>
velocity.properties配置:
HelloAction:
package com.myWeb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloAction {
@RequestMapping("/hello")
public String hello() {
System.out.println("hello");
return "a";
}
}