mercredi 28 mai 2014

Neither BindingResult nor plain target object for bean name 'newUser' available as request attribute


Vote count:

0




I’m new in Spring and trying to make a RegisterController for my Web portal. When I push button “Register” on my main page it shows this error:"Neither BindingResult nor plain target object for bean name 'newUser' available as request ". I’ve already read a lot of posts on this topic but any of them were helpful.


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


I tried to set commandName="newUser", but no difference.



<DIV class="registration_block">

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

<sf:label path="login"><strong>Enter Login:</strong></sf:label>
<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....

}


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire