lijiang658135 2015-10-23 02:17 采纳率: 0%
浏览 2632

Spring MVC集成velocity视图解析器问题

小弟最近学习搭建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";
    }

}

  • 写回答

1条回答 默认 最新

  • 邓苏镇 2018-07-31 01:40
    关注

    org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver

    修改为:
    org.springframework.web.servlet.view.velocity.VelocityViewResolver

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题