spring webmvc映射jsp(不带控制器)

VJ.*_*VJ. 3 spring-mvc

我试图抓住Spring 3 web-mvc.我有一个简单的页面链接(你知道.. <a href="xyz">事情.

不知何故,春天的mvc不喜欢那样..但是,我的弹簧配置不起作用,我希望它如何.

我尝试使用DefaultRequestToViewNameTranslator,但这没有帮助.我认为它与"处理程序"弹簧调度程序servlet选择的内容有关...但我还无法掌握这些东西.日志输出也没有多大帮助.

有人可以帮忙吗?

这是web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>

<session-config>
    <session-timeout>30</session-timeout>
</session-config> 

<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- Reads request input using UTF-8 encoding -->
<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>

<!-- Handles all requests into the application -->
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/appServlet/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

春天配置:

<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.mycompany.mvc" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="index"/>

<!-- Configures Handler Interceptors -->    
<mvc:interceptors>
    <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/images/**" location="/images/" />

<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/messages" />
    <property name="cacheSeconds" value="0" />
</bean>

<!-- Automatic resolution of the view names.. Convention over configuration -->
<bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/content/"/>
    <property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

jsp定义链接:

<li><a href="demo/flot">flot integration</a></li>
Run Code Online (Sandbox Code Playgroud)

并且日志文件输出:

DEBUG    o.s.web.servlet.DispatcherServlet:693 - DispatcherServlet with name 'Spring MVC Dispatcher Servlet' processing GET request for [/demo/flot]
 WARN         o.s.web.servlet.PageNotFound:947 - No mapping found for HTTP request with URI [/demo/flot] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
DEBUG    o.s.web.servlet.DispatcherServlet:674 - Successfully completed request
Run Code Online (Sandbox Code Playgroud)

Sla*_*hin 8

控制器应该处理用户的请求,在您的情况下,没有控制器映射到此URL.当控制器找到时,它会执行一些逻辑并返回视图名称,该名称将用于表示服务器的响应.因此,视图名称转换器仅在控制器之后调用,并且仅用于推导到特定JSP文件的完整路径.

尝试添加

<mvc:view-controller path="demo/flot" view-name="demo/flot"/>
Run Code Online (Sandbox Code Playgroud)

(另外,你可能会尝试省略view-name属性,但我不确定.)


VJ.*_*VJ. 5

目前..以下工作..虽然属性 /** 以后当我也添加控制器时可能对我来说是一个问题。

但我可以自定义 .jsp 文件 url

<bean id="handlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/**">urlFilenameViewController</prop>
        </props>
    </property>
</bean>
<bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" /> 
Run Code Online (Sandbox Code Playgroud)