Question

The getProductForm annotated with request mapping "/showproductform/edit" is not being invoked if there is an error

Instead its looking for a template with the name getProductForm "/showproductform/edit". I need to show the form with the error message if there is any error

Request Flow

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

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

@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;
    }
}

web.xml

            <?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>

App Context

    <?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> 
Was it helpful?

Solution

In your method addProduct you have this line:

return "/showproductform/edit"

At this point you have to return a template and not an URL. I think it should be "addproduct". But you have also to add the values (product) you got through the request to the Model, otherwise the form will be empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top