Search code examples
springjspspring-mvcthymeleafrequest-mapping

Spring MCV: Cannot handle data input


My goal is to authorization user via handling its login and password. I've tried to reproduce this example but faced with problem.

I have an Entity User class:

@DynamicUpdate
public class EntityUser
{
    String login;
    String password;

    public EntityUser() {}

    public EntityUser
    (
            String login,
            String password
        )
    {   
        this.login = login;
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

This is my .jsp file fragment:

<form action="#" th:action="@{/loginCheck}" th:object="${user}" method="post">
            <table border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" th:field="*{login}"/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" th:field="*{password}"/></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Login" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>
                </tbody>
            </table>
        </form>

And this is fragment of my Controller.java class:

@RequestMapping(value = "/loginCheck", method = RequestMethod.GET)
    public String userForm(Model model)
    {
        EntityUser user = new EntityUser();
        user.setLogin("login");
        user.setPassword("password");
        model.addAttribute("user", user);

        System.out.println(user.getLogin());
        System.out.println(user.getPassword());

        return "/loginCheck";
    }

    @RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
    public String processUser(@ModelAttribute(value="user") EntityUser user)
    {

        System.out.println(user.getLogin());
        System.out.println(user.getPassword());

        loginInfo = "Jakarta";
        return "redirect:/controllers";
    }

After input the values and pushing "Submit" button there is no GET or POST method called (there wasn't any prints in console), and the page is moving to /#

On the other hand, when I replace form action="#" to form action="/HelloSpringMVC/loginCheck", the POST method is called, but both strings printed are "null"

So, what's wrong there? Anybody knows?

EDITED: Here are my pom.xml and web.xml files.


Solution

  • Your pom.xml doesn't have the spring-boot-starter-thymeleaf dependency, so the thymeleaf tags are not being handled in your html/jsp templates. Add the thymeleaf starter dependency in your pom.xml and rebuild:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    

    Thymeleaf will automatically look for templates with .html extension in src/main/resources/templates. You may wish to customize the defaults by editing/creating src/main/resources/application.properties and adding any of the spring.thymeleaf.* application properties defined in http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html and tailor these for your own needs.