如果出现错误,则不会调用带有请求映射“/ showproductform / edit”的getProductForm
而是寻找名为getProductForm“/ showproductform / edit”的模板。 如果有任何错误,我需要显示带有错误消息的表单
Request 1 (GET) http://localhost:9002/site-admin-1.0/admin/showproductform/
Request 2 (POST) http://localhost:9002/site-admin-1.0/admin/addproduct
What behaviour I need is, if all the details in the product form
is not entered then we need to take the user back to the product
form with errors.
The URL would be
http://localhost:9002/site-admin-1.0/admin/showproductform/edit/
for which I am getting the error
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/showproductform/edit", template might not exist or might not be accessible by any of the configured Template Resolvers
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
@Controller
@SessionAttributes("products")
public class InventoryController {
@RequestMapping(value="/addproduct", method=RequestMethod.POST )
public String addProduct(@Valid Product product, BindingResult bindingResult, ModelMap modelMap) {
if(bindingResult.hasErrors()) {
return "/showproductform/edit";
}
int productAdded = this.productService.addProduct(product);
return "redirect:addproductsuccess";
}
@RequestMapping(value="/addproductsuccess", method=RequestMethod.GET)
public String onSuccess(String viewName) {
return viewName;
}
@RequestMapping(value={"/showproductform/edit","/showproductform"}, method=RequestMethod.GET)
public String getProductForm(ModelMap modelMap) {
if(!modelMap.containsKey("product")) {
modelMap.addAttribute("product", new Product());
}
return "addproduct";
}
@RequestMapping(value="/products", method=RequestMethod.GET)
public String listProducts(Model model) {
model.addAttribute("products", this.productService.getProducts());
return "products";
}
@ModelAttribute("categories")
public List<Category> populateCategories() {
return this.productService.getProductCategories();
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
binder.setValidator(new ProductValidator());
}
@Autowired
private ProductService productService;
public void setProductService(ProductService productService) {
this.productService = productService;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<description>Admin Web Descriptor</description>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/**/*spring-context.xml,classpath*:com/**/*spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.site" />
<!-- **************************************************************************** -->
<!-- ******************* Thymeleaf specific configuration *********************** -->
<!-- **************************************************************************** -->
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean id="templateResolver" class="org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver">
<property name="order" value="1"/>
<property name="prefix" value="/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="order" value="1" />
<property name="viewNames" value="*" />
</bean>
</beans>
答案 0 :(得分:1)
在你的方法addProduct中你有这一行:
return "/showproductform/edit"
此时您必须返回模板而不是URL。 我认为它应该是“addproduct”。 但您还必须将通过请求获得的值(产品)添加到模型中,否则表单将为空。