2012-11-14 50 views
7

我已经搜索了谷歌,stackoverflow和每个论坛,我可以看几天,我的键盘是在作为headbutt的目标的可怕风险。无XML的Spring 3.1没有找到HTTP请求的映射

我正在运行一个非常小的Spring 3.1 MVC和一个无XML的安装程序。问题是,当我启动它时,我看到了;

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/start.action],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.start(javax.servlet.http.HttpServletRequest) 
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/*],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.home(java.util.Locale,org.springframework.ui.Model) 

然而,当我试着打要么这些网址我看到我的控制器内火我的日志报表,然后立即获得的;

INFO : com.xxxxxx.info.HomeController - Welcome home! the client locale is en_US 
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/jsps/home.jsp] in DispatcherServlet with name 'dispatcher' 

这是我的源文件。

初始化器 -

public class Initializer implements WebApplicationInitializer { 

    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext(); 
     mvcContext.register(MvcConfig.class); 

     ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/*"); 
    } 
} 

MvcConfig -

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.xxxxxx.info")  
public class MvcConfig { 
    @Bean 
    public InternalResourceViewResolver configureInternalResourceViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("/WEB-INF/jsps/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 
} 

控制器 -

@Controller 
public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** Simply selects the home view to render by returning its name. */ 
    @RequestMapping(value = "/*") 
    public ModelAndView home(Locale locale, Model model) { 
     logger.info("Welcome home! the client locale is " + locale.toString()); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 
     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 
     return new ModelAndView("home", model.asMap()); 
    } 

    @RequestMapping(value = "/start.action") 
    public ModelAndView start(HttpServletRequest request) { 
     logger.info("Starting!"); 
     return new ModelAndView("start", null); 
    } 
} 

更改调度映射为 “/” 尽可能多的帖子中,我发现建议似乎完全打破它。如果我用“/”重新启动服务器,那么Spring没有报告任何内容,没有任何内容被映射,仅仅是一个tomcat启动日志,没有其他的工作。

我的文件结构 -

| com.xxxxxx.info 
    - Initializer.java 
    - MvcConfig.java 
    - HomeController.java 
| src 
    | main 
     | webapp 
      | WEB-INF 
       | jsps 
        - home.jsp 
        - start.jsp 

所以它似乎正确地打我的控制器,但是当它得到视图名称不解析到正确的地方。缺少什么我在这里,这似乎是简单的东西,我忽略了......

回答

3

我猜测,JSP正在由DispatcherServlet的呈现,它在similar question

讨论真实你应该考虑使用更具体映射在HomeControllerWebApplicationInitializer

+0

@Sarophym不清楚调度器servlet的/ mapping会出什么问题,你应该首先检查控制器映射是否适用于像'hello.html'这样的非根请求。 –

+0

非常感谢您的链接。那里的解决方案已经死了。我将调度程序代码更改为 'dispatcher.addMapping(“*。action”);' – Sarophym

3

您调度\*DispatcherServlet,以及你HomeController

dispatcher.addMapping("/*"); 

@RequestMapping(value = "/*") 
    public ModelAndView home(Locale locale, Model model) 

,我认为这是造成问题的原因。改变你的控制器映射到像

@RequestMapping(value = "/home") 
    public ModelAndView home(Locale locale, Model model) 

希望它有帮助。

+1

这并未解决问题,但我认为您遇到了正确的问题,结果发现更改调度程序映射解决了问题。 – Sarophym

相关问题