The requested resource is not available

Greetings! I just make a CRUD application, and I ran into the problem of updating users. When you click Save, the error "The requested resource is not available."

Information about the error and the request URL

UserController class

@Controller
public class UserControllerImpl implements UserController {

    private UserRepository repository;

    private UserValidator validator;

    @Autowired
    public UserControllerImpl(UserRepository repository, UserValidator validator) {
        this.repository = repository;
        this.validator = validator;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @Override
    public String getUsers(ModelMap model) {
        List<User> users = this.repository.listAll();
        model.addAttribute("user", users);
        return "index";
    }

    @RequestMapping(value = "addUser", method = RequestMethod.GET)
    @Override
    public String addUser(ModelMap model) {
        return "addUser";
    }

    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    @Override
    public String addUser(@RequestParam("name") String name, @RequestParam("age") String age, @RequestParam(value = "isAdmin",
            required = true, defaultValue = "false") Boolean isAdmin) {
        User user = new User(name, Integer.parseInt(age), isAdmin);
        user.setCreatedDate();
        this.repository.addUser(user);
        return "redirect:/";
    }

    @RequestMapping(value = "deleteUser/{id}", method = RequestMethod.GET)
    @Override
    public String deleteUser(@PathVariable Integer id) {
        this.repository.removeUser(id);
        return "redirect:/";
    }

    @RequestMapping(value = "updateUser/{id}", method = RequestMethod.GET)
    @Override
    public String getUpdate(@PathVariable Integer id, Model model) {
        model.addAttribute("userAttribute", this.repository.get(id));
        return "updateUser";
    }
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/application-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Dispatcher-servlet.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans >


    <context:component-scan base-package="com.temaprof.app.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Update.jsp-specifically, I have it and it does not work fully

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Update User</title>
</head>
<body>
<c:url var="saveUrl" value="/updateUser=${userAttribute.id}"/>
<form:form modelAttribute="userAttribute" method="post" action="${saveUrl}">
<table>
    <tr>
        <td><form:label path="id">Id:</form:label></td>
        <td><form:input path="id" disabled="true" /></td>
    </tr>

    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name"/></td>
    </tr>

    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age"/></td>
    </tr>
</table>
<input type="submit" value="Save"/>
</form:form>
</body>
</html>

There is also an index.jsp and addUser.jsp - everything works correctly in them. I will thank you for your help, if you do not have enough information-write what else you need)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Update User</title>
</head>
<body>
<c:url var="saveUrl" value="/updateUser/${userAttribute.id}"/>
<form:form modelAttribute="userAttribute" method="post" action="${saveUrl}">
<table>
    <tr>
        <td><form:label path="id">Id:</form:label></td>
        <td><form:input path="id" disabled="true" /></td>
    </tr>

    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name"/></td>
    </tr>

    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age"/></td>
    </tr>

    <%--<tr>--%>
        <%--<td><form:label path="isAdmin">Admin</form:label></td>--%>
        <%--<td><form:checkbox path="isAdmin"/></td>--%>
    <%--</tr>--%>
</table>
<input type="submit" value="Save"/>
</form:form>
</body>
</html>
Author: Mikhail Vaysman, 2017-03-24

1 answers

You have UpdateUser waiting for id after /. And in the picture you have=

@RequestMapping(value = "updateUser/{id}", method = RequestMethod.GET)
@Override
public String getUpdate(@PathVariable Integer id, Model model) {
    model.addAttribute("userAttribute", this.repository.get(id));
    return "updateUser";
}

@RequestMapping(value = "updateUser/{id}", method = RequestMethod.POST)
@Override
public String postUpdate(@PathVariable Integer id, Model model) {
    model.addAttribute("userAttribute", this.repository.get(id));
    return "updateUser";
}
 0
Author: Mikhail Vaysman, 2017-03-24 17:14:29