我已經搜索了谷歌,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
所以它似乎正確地打我的控制器,但是當它得到視圖名稱不解析到正確的地方。缺少什麼我在這裏,這似乎是簡單的東西,我忽略了......
@Sarophym不清楚調度器servlet的/ mapping會出什麼問題,你應該首先檢查控制器映射是否適用於像'hello.html'這樣的非根請求。 –
非常感謝您的鏈接。那裏的解決方案已經死了。我將調度程序代碼更改爲 'dispatcher.addMapping(「*。action」);' – Sarophym