dimanche 19 mai 2019

Java Spring MVC Several Pages

I work with Spring MVC. Everything is ok but I have an error when I try to check if logged in admin or person, if logged in person the new page has opened but when admin then I got an error like Request processing failed; nested exception is java.lang.NullPointerException and description is The server encountered an unexpected condition that prevented it from fulfilling the request. This error points to this if statement on the controller. What did I write wrong? Maybe I should divide the method printResult()? Please help me.

This is my controller:

@RequestMapping("")
@Controller
public class HelloController {
    private static final Logger logger = Logger.getLogger(HelloController.class.getName());

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ModelAndView printHello() {
        return new ModelAndView("hello", "command", new LoginPerson());
    }

    @RequestMapping(value = "/res",method = RequestMethod.POST)
    public String printResult(@ModelAttribute("SpringWeb")LoginPerson loginPerson, ModelMap model) {
        model.addAttribute("email", loginPerson.getEmail());
        model.addAttribute("password", loginPerson.getPassword());


        if loginPerson.getEmail().equals("admin@admin.com") && loginPerson.getPassword().equals("admin")) {
            logger.warning("Test Admin");
            return "welcomeadmin";
        } else {
            logger.warning("Test Person");
            logger.warning(loginPerson.getEmail());
            return "welcomeperson";
        }
    }
}

And this is my LoginPerson.java

public class LoginPerson {
    public String email;
    public String password;

    public void setEmail(String email) {
        this.email = email;
    }

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

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }
}

This is my hello.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form:form method="post" action="/springlab_war_exploded/res">
        <fieldset>
            <div id="legend">
                <center>
                    <legend class="">Log in</legend>
                </center>
            </div>
            <div class="control-label">
                <!-- Email -->
                <label class="control-label" for="email">Email:</label>
                <div class="controls">
                    <input type="email" id="email" name="email" placeholder="" class="input-xlarge">
                    <p class="help-block">Please provide your Email</p>
                </div>
            </div>
            <div class="control-group">
                <!-- Password-->
                <label class="control-label" for="pass">Password:</label>
                <div class="controls">
                    <input type="password" id="pass" name="pass" placeholder="" class="input-xlarge">
                    <p class="help-block">Password should be at least 4 characters</p>
                </div>
            </div>
            <div class="control-group">
                <!-- Button -->
                <div class="controls">
                    <button class="btn btn-success">Go</button>
                </div>
            </div>
        </fieldset>
    </form:form>
</body>
</html>

This is my welcomeadmin.jsp the same is welcomeperson.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>Welcome Admin</h1>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire