我是Spring的新手,正在尝试为我的Web门户创建RegisterController。
当我在主页上按下“注册”按钮时,它显示此错误:“ BeanResult'newUser'的BindingResult或普通目标对象都不能作为请求使用”。
 我已经阅读了很多有关此主题的文章,但其中任何一个都对您有所帮助。

RegisterController.class

import com.springapp.Entity.User;
import com.springapp.Service.UserService.UserService;
import com.springapp.mvc.RegisterForm;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping(value = "/register.do")
public class RegisterController {
//Log4j
private static final Logger logger = Logger.getLogger(RegisterController.class);

@Qualifier("userServiceImpl")
@Autowired
private UserService userService;

private void putNewUserForm(ModelMap model) {
    RegisterForm newUser = new RegisterForm();
    model.addAttribute("newUser", newUser);
}

@RequestMapping(method = RequestMethod.GET)
public String register(ModelMap model) {
    putNewUserForm(model);
    return "register";
}

@RequestMapping(method = RequestMethod.POST)
public String processRegister(@ModelAttribute("newUser")
       @Valid final RegisterForm  newUser, final BindingResult result,
       Model model, HttpServletRequest request) {
    if (result.hasErrors()) {
        logger.debug("result has errors!");
        return "register";
    }
    if (createUser(newUser, model)) {
        return "register";
    }
    return "welcome";
}

private boolean createUser(RegisterForm newUser, Model model) {
    User user = newUser.getUser();
    userService.insertUser(user);
    return false;
 }
}


Register.jsp

我试图设置commandName =“ newUser”,但没有区别。

<DIV class="registration_block">

           <sf:form method="POST" modelAttribute="newUser" action="${app}/register.do">

                <sf:label path="login"><strong>Enter Login:</strong></sf:label>
                <sf:input path="login" type="text" size="20"/><br>
                <span class="error"><form:errors path="login"/></span>
                <br>

                .........

                <input type="SUBMIT" name="SUBMIT" value="Register"/>
            </sf:form>


RegisterForm.class

 import com.springapp.Entity.User;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.validator.constraints.Email;
 import org.hibernate.validator.constraints.NotEmpty;
 import javax.validation.constraints.Pattern;
 import javax.validation.constraints.Size;
 import java.sql.Timestamp;


public class RegisterForm {
private static final Log logger = LogFactory.getLog(RegisterForm.class);

private Integer userId;

@NotEmpty
@Size(min = 2, max = 25)
@Pattern(regexp = "^([a-zA-Z0-9_-])+$")
private String login;

@NotEmpty
@Size(min = 2, max = 25)
@Pattern(regexp = "^([a-zA-Z])+$")
private String name;

@NotEmpty
@Size(min = 2, max = 25)
@Pattern(regexp = "^([a-zA-Z0-9_-])+$")

private String password;

@NotEmpty
@Size(min = 2, max = 25)
private String passwordConfirm;

@NotEmpty
@Email
@Pattern(regexp = "^([a-zA-Z0-9_\\.\\-+])+@(([a-zA-Z0-9-])+\\.)+([a-zA-Z0-9]{2,4})+$")
private String email;

@NotEmpty
@Email
private String emailConfirm;

private Timestamp regDate;

private String role;


public RegisterForm() {
}


public User getUser() {
    User user = new User();
    user.setUserId(userId);
    logger.trace("set user 'login'" + userId);
    user.setLogin(login);
    logger.trace("set user 'email'" + login);
    user.setEmail(email);
    logger.trace("set user 'firstName'" + email);
    user.setName(name);
    logger.trace("set user 'password'" + name);
    user.setPassword(password);
    logger.trace("set user 'password'" + password);
    user.setRegDate(regDate);
    logger.trace("set user regDate'" + regDate);
    return user;
}


public void setUser(User user) {
    logger.trace("Constructor " + user);
    this.userId = user.getUserId();
    logger.trace("Get 'id' " + getUserId());
    this.login = user.getLogin();
    logger.trace("Get 'login' " + user.getLogin());
    this.password = user.getPassword();
    logger.trace("Get 'password' " + user.getPassword());
    this.name = user.getName();
    logger.trace("get 'Name'" + name);
    this.email = user.getEmail();
    logger.trace("Get 'email' " + user.getEmail());
    this.regDate = user.getRegDate();
    logger.trace("Get 'regDate' " + user.getRegDate());
   }

 //Getters and Setters....

}


MainController.class

import com.springapp.Service.UserService.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

    @Controller
    @RequestMapping("/")
    public class MainController {
    private static final Log logger = LogFactory.getLog(MainController.class);

    @Qualifier("userServiceImpl")
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showIndexPage() {
        return "login";
    }

    @RequestMapping(value = "/login")
    public String showLoginPage() {
        return "login";
    }

    @RequestMapping(value = "/welcome")
    public String userWelcomePage() {
      return "welcome";
    }


    @RequestMapping(value = "/viewall", method = RequestMethod.GET)
    private ModelAndView listUsers(ModelAndView model) {
        return model.addObject("members", userService.getAllUsers());
    }

    @RequestMapping("delete")
    public ModelAndView deleteUser(@RequestParam("userId") Integer userId) {
        ModelAndView mav = new ModelAndView("redirect:/viewall");
        userService.deleteUser(userId);
        return mav;
    }


    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String userEdit() {
         return "adminUserUpdate";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    private ModelAndView registerUser(ModelAndView model) {
        return model.addObject("register");
    }
}

最佳答案

好吧,我已经解决了这个问题。它在RequestMapping中。
这是有效的代码。尚不完美,但它显示RegisterPage并允许提交。

 import com.springapp.Entity.User;
 import com.springapp.Service.RoleService.RoleService;
 import com.springapp.Service.UserService.UserService;
 import com.springapp.mvc.RegisterForm;
 import com.springapp.mvc.UserValidator;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.ui.ModelMap;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.servlet.ModelAndView;

 import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;

 @Controller
 public class RegisterController {
 //Log4j
 private static final Logger logger = Logger.getLogger(RegisterController.class);


 @Qualifier("userServiceImpl")
 @Autowired
 private UserService userService;

 @Qualifier("roleServiceImpl")
 @Autowired
 private RoleService roleService;


 private void putNewUserForm(ModelAndView model) {
     RegisterForm newUser = new RegisterForm();
     model.addObject("newUser", newUser);
 }

 @RequestMapping(value = "/register", method = RequestMethod.GET)
 public String register(ModelMap model) {
    model.addAttribute("newUser", new RegisterForm());
     return "register";
 }

 @RequestMapping(value = "/register.do", method = RequestMethod.POST)
 public String sayHello(RegisterForm newUser, Model model) {
     model.addAttribute("newUser", newUser);

    if (createUser(newUser, model)) {
        return "register";
    }
    return "welcome";
}

private boolean createUser(RegisterForm newUser, Model modelAndView) {
    User user = newUser.getUser();
    user.setRole(roleService.findByName(newUser.getRole()));
    userService.insertUser(user);
    return false;
}

关于java - BeanResult'newUser'的BindingResult和普通目标对象都不能用作请求属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23913646/

10-11 17:53