简体   繁体   English

单击提交按钮时无操作,但应注册用户

[英]No action when submit button is clicked, but it should register a user

I am trying to create a simple registration and login system. 我正在尝试创建一个简单的注册和登录系统。 When I am trying to register a user I am providing all informations to the input such as username, password and confirmpassword, but when I hit the submit button it redirect me to the /login even it should to /welcome page as I declared in the controller. 当我尝试注册用户时,我提供了输入的所有信息,例如用户名,密码和确认密码,但是当我点击提交按钮时,它会将我重定向到/ login,即使它应该是/ welcome页面,因为我在控制器。 When I added a simple debug in the controller in the specific mapping, nothing happened I suppose that the method is not even executed, could someone take a look at this code and tell me what is wrong here? 当我在特定映射中的控制器中添加一个简单的调试时,没有发生任何事情,我认为该方法甚至没有执行,有人可以看一下这段代码并告诉我这里有什么问题吗?


@Controller
public class UserController {

    private UserService userService;
    private SecurityService securityService;

    @Autowired
    public UserController(UserService userService, SecurityService securityService) {
        this.userService = userService;
        this.securityService = securityService;
    }

    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String registration(Model model) {
//        model.addAttribute("user", new User());
//
        return "registration";
    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public String registration(@RequestParam("username") String username, @RequestParam("password")
                               String password, @RequestParam("confirmPassword") String confirmPassword,
                               Model model, HttpServletRequest request) {

        User foundUser = userService.findByUsername(username);
        if (foundUser != null) {
            model.addAttribute("msgError", "That user already exists");
            return "registration";
        }

        if (username.equals("") || username.length() < 2) {
            model.addAttribute("msgError", "Wrong username");
            return "registration";
        }

        if (!password.equals(confirmPassword)) {
            model.addAttribute("msgError", "Passwords can not be different");
            return "registration";
        }

        User user = new User(request.getParameter("username"), request.getParameter("password"), request.getParameter("confirmPassword"));

        securityService.autoLogin(username, password);

        userService.addUser(user);
        model.addAttribute("msgSuccess", "User registered successfully");
        return "redirect:/welcome";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model, String error, String logout) {
        if (error != null)
            model.addAttribute("msgError", "Your username and password is invalid");
        if (logout != null)
            model.addAttribute("msgError", "You have been logged out successfully");

        return "login";
    }
}

And the JSP for the registration is as below 注册的JSP如下所示

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Watchers - Registration</title>
</head>
<body>
    <div class="container">
        <form method="POST" action="registration" class="sign-form">
            <h2 class="sign-form-heading">Create an new account</h2>
            <p><input type="text" name="username" /></p>
            <p><input type="text" name="password" /></p>
            <p><input type="text" name="confirmPassword" /></p>
            <input type="submit" value="Submit" />
        </form>
    </div>
</body>
</html>

i think it's because of your forms action -value. 我认为这是因为你的表单action值。

try to prepare the action value with spring's taglib in this example its got the prefix spring 尝试使用spring的taglib准备动作值,在此示例中,它获得了前缀spring

<spring:url value="/registration" var="url"/>
<form method="POST" action="${url}" class="sign-form">

Edit: 编辑:

referring to your comment, i've got to ask a few questions: 提到你的评论,我要问几个问题:

how do you protect/secure your web-app, with spring-security or security-constraints in web.xml? 如何使用web.xml中的spring-security或security-constraints来保护/保护您的Web应用程序?

is the /registration -path also protected/secured? /registration -path是否也受到保护/保护? if it is and if your login-config refers to /login , you will be redirected to /login 如果是,如果您的login-config引用/login ,您将被重定向到/login

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM