我正在学习spring / spring mvc,现在正在准备登录和注册页面。我有2个jsp文件。
public class Adaptarecycler extends RecyclerView.Adapter<Adaptarecycler.ViewHolder> {
List<Juego> jueguis;
private Context mcontext;
public Adaptarecycler(Context mcontext,List<Juego> jueguis){
this.jueguis = jueguis;
this.mcontext = mcontext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_juego, viewGroup, false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder( final ViewHolder viewHolder,final int i) {
final Juego j1 = jueguis.get(i);
viewHolder.titltxt.setText(j1.getTitulo());
viewHolder.titltxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mcontext, Addmod.class);
//Bundle b = new Bundle();
//b.putParcelable("juego",j1);
i.putExtra("jue",j1);
mcontext.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return jueguis.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView titltxt;
public ViewHolder( View itemView) {
super(itemView);
titltxt=(TextView) itemView.findViewById(R.id.titltxt);
}
}
}
和login.jsp
注册成功后,我想将用户重定向到registration.jsp.
并显示消息,例如。 “已创建帐户。您可以立即登录” 。创建用户并将其保存到数据库后,无法显示添加到模型的模型属性。重定向后,该模型属性(successMessage)应该显示在login.jsp
上。我可以在login.jsp
上显示消息,但这不是我想要实现的。
这是我的registration.jsp
login.jsp
这是我的POST控制器:
<%@ include file="common/header-logout.jspf" %>
<div class="container">
<form method="POST" action="${contextPath}/login" class="form-signin">
<h2 class="form-heading">Log in</h2>
<div class="form-group ${error != null ? 'has-error' : ''}">
<span>${message}</span>
<span>${error}</span>
<input name="username" type="text" class="form-control" placeholder="Username"/>
<input name="password" type="password" class="form-control" placeholder="Password"/>
<button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
<h4 class="text-center"><a href="${contextPath}/registration">Create an account</a></h4>
</div>
</form>
<h2><span class="text-success">Here I want to display ${successMessage}</span></h2>
</div>
<%@ include file="common/footer.jspf" %>
创建用户后添加的模型属性“ successMessage”应显示在 @RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") @Valid User userForm, BindingResult bindingResult, ModelMap model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
LOG.info("User created with success: " + userForm.getUsername() + " with roles: " + userForm.getRoles());
model.addAttribute("successMessage", "User has been registered successfully");
return "redirect:/login";
}
中。不幸的是$ {successMessage}在login.jsp
中不可见。只能在login.jsp
中访问。