I'm having some problems trying to submit a form in Spring. Every time I try I get a 404 error. This is my controller:
@Controller
@RequestMapping("/admin/news")
public class NewsController {
@Autowired
private NewsService newsService;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createNews(Model model) {
return "admin.news.create";
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView listNews(Model model) {
return listNews(1);
}
@RequestMapping(value = "/list/{pageIndex}", method = RequestMethod.GET)
public ModelAndView listNewsPage(@PathVariable int pageIndex) {
return listNews(pageIndex);
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveNews(@Valid NewsDto newsDto, BindingResult bindingResult, Model model) {
if (!bindingResult.hasErrors()) {
newsService.save(newsDto);
model.addAttribute("created", true);
model.addAttribute("news", newsDto);
}
return "admin.news.create";
}
private ModelAndView listNews(int pageIndex) {
ModelAndView modelAndView = new ModelAndView("admin.news.list");
Page page = newsService.findAll(pageIndex);
modelAndView.addObject("page", page);
return modelAndView;
}
@ModelAttribute("news")
private NewsDto newsDtoModel() {
return new NewsDto();
}
}
And this is the form (I'm using Apache Tiles):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="spring" %>
<div id="page-header">
<h3>Create a news</h3>
</div>
<div id="page-content">
<div class="panel">
<div class="panel-heading">
News creation form
</div>
<div class="panel-content">
<spring:form method="post" commandName="news" action="save">
<spring:input path="title" class="u-full-width" type="text" placeholder="Title of the news" required="true" />
<spring:errors path="title" cssClass="validation-error" />
<spring:textarea path="text" class="u-full-width" placeholder="News text" required="true" />
<spring:errors path="text" cssClass="validation-error" />
<input class="button-primary" type="submit" value="Submit" />
</spring:form>
</div>
</div>
</div>
I've read and tried all the solutions that I've found in SO related with this error without luck and I've also tried all possible route combinations in the form action but I always get that 404 error. Here is the header:
GET /admin/news/save HTTP/1.1 Host: myserver:8080 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8,es;q=0.6 Cookie: JSESSIONID=2A3446E8A0A42C49B0ECF8EE2C38C917 DNT: 1 Referer: myserver:8080/admin User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
HTTP/1.1 404 Not Found Cache-Control: no-cache, no-store, max-age=0, must-revalidate Content-Language: en Content-Length: 1000 Content-Type: text/html;charset=UTF-8 Date: Fri, 19 Dec 2014 17:11:24 GMT Expires: 0 Pragma: no-cache Server: Apache-Coyote/1.1 X-Content-Type-Options: nosniff X-Frame-Options: DENY X-XSS-Protection: 1; mode=block
Any idea?
EDIT 1: I'll add here my configs.
ApplicationContext:
@Configuration
@ComponentScan("com.davidmogar.alsa")
@PropertySource("classpath:application.properties")
@Import({WebMvcContext.class, PersistenceContext.class, SecurityContext.class})
public class ApplicationContext {
private static final String MESSAGE_SOURCE_BASE_NAME = "i18n/messages";
@Bean
MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE_BASE_NAME);
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
@Bean
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
WebAppInitializer:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
private static final String CHARACTER_ENCODING_FILTER_ENCODING = "UTF-8";
private static final String DISPATCHER_SERVLET_MAPPING = "/";
@Override
protected WebApplicationContext createRootApplicationContext() {
WebApplicationContext context = super.createRootApplicationContext();
// ConfigurableEnvironment env = (ConfigurableEnvironment) context.getEnvironment();
// env.setActiveProfiles();
return context;
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{ApplicationContext.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{DISPATCHER_SERVLET_MAPPING};
}
@Override
protected Filter[] getServletFilters() {
DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
delegatingFilterProxy.setTargetBeanName("springSecurityFilterChain");
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding(CHARACTER_ENCODING_FILTER_ENCODING);
characterEncodingFilter.setForceEncoding(true);
OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter();
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{delegatingFilterProxy, characterEncodingFilter, openEntityManagerInViewFilter,
hiddenHttpMethodFilter};
}
}
EDIT 2:
Here is the mapping of the URL:
INFO - questMappingHandlerMapping - Mapped "{[/admin/news/save],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.davidmogar.alsa.web.admin.news.NewsController.saveNews(com.davidmogar.alsa.dto.news.NewsDto,org.springframework.validation.BindingResult,org.springframework.ui.Model)