【发布时间】:2014-06-27 15:40:53
【问题描述】:
我过去曾与 Spring 合作过一个大型项目,但我从未从头开始创建 Spring MVC Web 应用程序。
这就是我目前正在做的练习,因为我有一个项目需要它。
我成功地制作了一个使用 .JSP 页面(使用注释,没有 XML)的简单 Spring MVC Web 应用程序。 不过我想使用 Thymeleaf 并开始了我的转换过程。
好吧,现在我遇到了 404 错误,而且我的 HomeController 类似乎没有被击中。
我在控制台输出中没有收到任何错误。
我有 Google 搜索、通读教程和代码示例。第二双眼睛会很好。谢谢! :)
注意:从 .JSP 到 Thymeleaf 所做的唯一更改是添加了 ThymeleafConfig 类。我不明白它是如何从工作变成不工作的。
这是我的代码:
WebInit.java
public class WebInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Creates the root application context
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ServletConfig.class);
appContext.setDisplayName("REPLACE ME");
appContext.setConfigLocation("com.demo.config");
// Creates the Spring Container shared by all Servlets and Filters
servletContext.addListener(new ContextLoaderListener(appContext));
// Further configures the servlet context
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.setAsyncSupported(true);
dispatcher.addMapping("/");
}
}
ServetConfig.java
@Configuration
@Import(WebConfig.class)
@ImportResource({/*"classpath:META-INF/spring/persistence-context.xml"*/})
public class ServletConfig {
}
WebConfig.java
@Configuration
@ComponentScan("com.illinois.dnr")
@EnableAspectJAutoProxy
@EnableWebMvc
@Import({ThymeleafConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter {
// Maps resources path to webapp/resources
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
// Provides internationalization of messages
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}
}
Thymeleaf.java
@Configuration
public class ThymeleafConfig {
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}
}
HomeController.java
@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 = "/dnr", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(Locale locale, Model model) {
logger.info("Login");
return "login";
}
@RequestMapping(value = "/home", method = RequestMethod.POST)
public String login(@Validated User user, Model model) {
model.addAttribute("userName", user.getUserName());
logger.info("User");
return "user";
}
@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is welcome page!");
model.setViewName("hello");
logger.info("Hello");
return model;
}
@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is protected page - Admin Page!");
model.setViewName("admin");
logger.info("admin");
return model;
}
@RequestMapping(value = "/dba**", method = RequestMethod.GET)
public ModelAndView dbaPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is protected page - Database Page!");
model.setViewName("admin");
logger.info("dba");
return model;
}
}
【问题讨论】:
-
使用 STS 和 tomcat7
-
有堆栈跟踪可言吗?另外,你能不能把
System.out.println()'s in yourThymeleafConfig`方法来确保它们正在运行。你所有的 Thymeleaf 模板都在 WEB-INF/views 中吗? -
记住你应该去localhost:8080/appName/dnr 并且页面是WEB-INF/views/dnr.html
-
CodeChimp:感谢在我的方法中运行 System.out 的想法!我自己应该想到的。
标签: java spring spring-mvc http-status-code-404 thymeleaf