2017-02-12 115 views

回答

2

要配置用SpringMVC有两种方式XML配置注释配置

  1. 使用XML(这是旧的方式,但不建议再)配置:

spring-mvc-config.xml:这里我们将/hello映射到helloWorldController

<beans ...> 

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
     <props> 
      <prop key="/hello">helloWorldController</prop> 
     </props> 
     </property> 
    </bean> 

    <bean id="helloWorldController" class="xx.yy.zz.HelloWorldController" /> 

</beans> 

HelloWorldController应该从AbstractController延伸并实现handleRequestInternal()

公共类HelloWorldController延伸一个AbstractController {

@Override 
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { 

     ModelAndView model = new ModelAndView("hello"); 

     model.addObject("message", "HelloWorld!"); 

     return model; //will go to hello.jsp 
    } 
} 
  • 的这个与注解等效config
  • @Controller 
    public class HelloWorldController 
    { 
    
        @RequestMapping("/hello") 
        protected ModelAndView hello() throws Exception { 
    
         ModelAndView model = new ModelAndView("hello"); 
    
         model.addObject("message", "HelloWorld!"); 
    
         return model; //will go to hello.jsp 
        } 
    } 
    
    +0

    我们在哪写这个xml文件在hybris的意思是哪个扩展名? –

    +0

    storefront \ web \ webroot \ WEB-INF \ config \ spring-mvc-config.xml –

    +0

    感谢提供的信息 –