我已经搜索了谷歌,stackoverflow,以及我可以看几天的每个论坛,我的键盘很可能成为头部的目标.
我正在运行一个非常小的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 "{[/*],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.home(java.util.Locale,org.springframework.ui.Model)
然而,当我尝试点击这些URL中的任何一个时,我看到我的控制器内的日志语句会激活然后立即获取;
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
因此它似乎正确地击中了我的控制器,但是当它获得视图名称时它没有解析到正确的位置.我在这里想念的是什么,这似乎是我忽略的简单……
解决方法
我猜这个jsp是由DispatcherServlet呈现的,它在
similar question中讨论过
您应该考虑在HomeController和WebApplicationInitializer中使用更具体的映射.