spring webmvc mapping the jsp (without controllers)

后端 未结 6 1564
星月不相逢
星月不相逢 2021-01-03 09:47

I am trying to get my hands on Spring 3 web-mvc. I have a simple page link (you know.. thing.

Somehow spring mvc doesn\'t like th

相关标签:
6条回答
  • 2021-01-03 10:24

    Putting

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

    in application-context.xml resolved the issue for me. The view which does not have a controller(request mapping) will not load if we put these things in servlet.xml. I tried putting these things in application-context.xml and it worked for me. Thanks!!!

    0 讨论(0)
  • 2021-01-03 10:28

    I am using spring web 3.2.1 and I have only internal resolver and annotations based config. The jsp files view are accessible by default without using controller i.e not coded in any controller at return value of the mapped controller method. I guess there is some implicit rule.

    0 讨论(0)
  • 2021-01-03 10:29

    Using spring-webmvc 3.0.6.RELEASE this worked for me:

    <!-- 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>
    

    Beans annotated with @Controller and @RequestMapping are called on the appropriate url, any url like secure/* and secure/extreme/* is handled by WEB-INF/views/secure/index.jsp and WEB-INF/views/secure/extreme/index.jsp respectively

    0 讨论(0)
  • 2021-01-03 10:39

    Controller should handle user's request and in your case no controller which mapped to this URL. When controller found, it performs some logic and returns view name which will be used to represent server's response. So, view name translator called only after controller and only for deduce full path to particular JSP file.

    Try to add

    <mvc:view-controller path="demo/flot" view-name="demo/flot"/>
    

    (Also, you probably may try to omit view-name attribute, but I don't sure.)

    0 讨论(0)
  • 2021-01-03 10:41

    Currently.. following worked.. Though the property /** might be an issue for me later when I add the controllers too.

    But I can customize the .jsp file 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" /> 
    
    0 讨论(0)
  • 2021-01-03 10:42

    After version 3.1 there is WebMvcConfigurerAdapter, so you can put the mapping in configuration like this:

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/index");
      } 
    }
    
    0 讨论(0)
提交回复
热议问题