我正试图抓住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>
spring config :
<!-- 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>
jsp ,其中定义了链接:
<li><a href="demo/flot">flot integration</a></li>
日志文件输出:
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
答案 0 :(得分:8)
控制器应该处理用户的请求,在您的情况下,没有控制器映射到此URL。当控制器找到时,它会执行一些逻辑并返回视图名称,该名称将用于表示服务器的响应。因此,视图名称转换器仅在控制器之后调用,并且仅用于推导到特定JSP文件的完整路径。
尝试添加
<mvc:view-controller path="demo/flot" view-name="demo/flot"/>
(另外,您可能会尝试省略view-name
属性,但我不确定。)
答案 1 :(得分:4)
目前..以下工作.. 虽然稍后我添加控制器时属性/ **可能对我来说是一个问题。
但我可以自定义.jsp文件网址
<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" />
答案 2 :(得分:3)
使用spring-webmvc 3.0.6.RELEASE这对我有用:
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<view-controller path="secure/*" view-name="secure/index"/>
<view-controller path="secure/extreme/*" view-name="secure/extreme/index"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
使用@Controller和@RequestMapping注释的Bean在相应的url上调用,任何url如secure / *和secure / extreme / *由WEB-INF / views / secure / index.jsp和WEB-INF / views处理/secure/extreme/index.jsp分别
答案 3 :(得分:0)
我正在使用spring web 3.2.1,我只有内部解析器和基于注释的配置。默认情况下可以访问jsp文件视图而不使用控制器,即在映射控制器方法的返回值处未在任何控制器中编码。我想有一些隐含的规则。
答案 4 :(得分:0)
把
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
为我解决了这个问题。如果我们将这些东西放在servlet.xml中,那么没有控制器(请求映射)的视图将不会加载。我尝试将这些东西放在application-context.xml中,它对我有用。感谢!!!
答案 5 :(得分:0)
在版本3.1之后有WebMvcConfigurerAdapter,因此您可以将映射放在配置中,如下所示:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index");
}
}