我在工作中继承了一个spring 4 web项目,该项目配置了Tiles视图框架,用于调度程序servlet和IOC的Spring框架。
我正在尝试构建一个重置密码功能,它有三个屏幕,有三种形式。
1)重置表格。使用户的电子邮件和控制器方法发送带有临时密码的电子邮件。
public class MainVM
{
public ObservableCollection<CategoryItem> Categories { get; set; }
public MainVM()
{
Categories = new ObservableCollection<CategoryItem>();
}
}
public class CategoryItem
{
public string Name { get; set; }
}
2)表格输入temp pwd:
<form:form class="form-horizontal" action="/NCHP/reset.htm" method="post" id="resetForm">
<fieldset>
<legend>Enter your registered email address</legend>
<div class="form-group">
<div class="col-sm-12">
<input id="resetEmail" name="email" class="form-control" placeholder="Email" required="required" tabindex="1" type="email">
</div>
</div>
<div class="form-group">
<div class="col-sm-12 ">
<span class="pull-right">
<button type="reset" class="btn btn-default" data-dismiss="modal">Back</button>
<button type="submit" class="btn btn-primary" >Submit</button>
</span>
</div>
</div>
</fieldset>
</form:form>
3)表格输入新密码:
<form:form class="form-horizontal" name="verify" method="POST" id='verifyform'>
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-user form-control-feedback"></i>
<input type="text" id="inputEmailVerify" name="userNameVerify" placeholder="User Name" class="form-control" autofocus="autofocus">
</div>
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-lock form-control-feedback"></i>
<input type="password" name="passwordVerify" id="inputPasswordVerify" placeholder="Password" class="form-control" autofocus="autofocus">
</div>
<div class="form-group" id="verify-btn">
<div class="col-sm-12 ">
<span class="pull-right">
<input type="submit" formaction="/NCHP/resetVerify.htm" value="Verify" class="btn btn-primary btn-block">
</span>
</div>
</div>
</form:form>
前两个表单我做一个ajax提交,只返回数据而没有像下面这样的jquery $ .ajax()
<form:form class="form-horizontal" name="resetPass" method="POST" id='resetPassForm' >
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-lock form-control-feedback"></i>
<input type="password" id="newpass" name="newpass" placeholder="new Password" class="form-control" autofocus="autofocus">
</div>
<div class="form-group has-feedback">
<i class="glyphicon glyphicon-lock form-control-feedback"></i>
<input type="password" name="newpass2" id="newpass2" placeholder="re enter Password" class="form-control" autofocus="autofocus">
</div>
<div class="form-group" id="new-login-btn">
<div class="col-sm-12 ">
<span class="pull-right">
<input type="submit" formaction="/NCHP/changePass.htm" value="Save Password" onsubmit="modaljay();" class="btn btn-info btn-block">
<div class="modaljay"><!-- Place at bottom of page --></div>
</span>
</div>
</div>
</form:form>
第三种形式是常规表单提交,没有ajax提交。因为我希望用户在使用temp登录并设置新密码后转发到他们的主页。
以下是前两种表格的控制器映射。
$('#resetForm').submit(function(event) {
$("#verifyModal").hide();
$("passModal").hide();
var resetmail = $('#resetEmail').val();
console.log(resetmail);
var json = {
"email" : resetmail
};
$.ajax({
url : "http://localhost:8080/NCHP/reset.htm",
data : json,
dataType : 'text json',
type : "POST",
success : function(e) {
$('#resetModal').modal('hide');
$("#verifyModal").modal('show');
console.log(e);
},
error : function(e) {
console.log(e);
if (e.responseText == "email sent") {
$('#resetModal').modal('hide');
$("#verifyModal").modal('show');
} else if (e.responseText == "email not sent. UNAUTHORIZED") {
$("#emailResponce").html("there was an error. We could not process your request at this time. Please contact support@nexiscard.com");
}
}
});
event.preventDefault();
});
}
和第三种形式。
@Controller
public class UserLoginreset
{
@Autowired
public UserDao userDao;
@Autowired
public BCryptPasswordEncoder encoder;
@Autowired
public SecureServiceClient serviceClient;
@RequestMapping(value = "/reset", method = RequestMethod.POST)
public @ResponseBody String reset( @RequestBody(required=true) String email,
HttpServletRequest request)
{
String mail="";
try{
mail = java.net.URLDecoder.decode(email, "UTF-8").replace("email=", "");
}
catch (UnsupportedEncodingException e){
e.printStackTrace();
}
User user =userDao.findUserByemail(mail);
request.getSession().setAttribute("user",user);
String resetPwd= encoder.encode("xxxx");
if(user!=null && email!=null){
userDao.resetPass(user.getUser_name(), resetPwd);
return (String) serviceClient.returnRestTemplate("email", mail);//"home";
} else {
return "login";//"index";
}
}
@RequestMapping(value = "/resetVerify", method = RequestMethod.POST)
public @ResponseBody String resetVerify( @RequestParam(value="user",required=true) String user,@RequestParam(value="pass",required=true) String pass,
HttpServletRequest request)
{
User user1 =userDao.findUserByName(user);
if(user1!=null && pass.matches("xxxx")){
return "success";//"home";
} else {
return "unauthorised";//"index";
}
}
以下是我的春季配置
@RequestMapping(value = "/changePass", method = RequestMethod.POST)
public @ResponseBody String resetPass( @RequestParam(value="newpass", required=true) String newpass, @RequestParam(value="newpass2", required=true) String newpass2,
HttpServletRequest request, Model model, HttpServletResponse httpServletResponse)
{
User user = (User)request.getSession(false).getAttribute("user");
if(user.getUser_name()!=null && newpass.matches(newpass2)){
userDao.resetPass(user.getUser_name(), encoder.encode(newpass));
String news = userDao.getDynamicNewsByUser(user.getUser_name());
model.addAttribute("user",user);
model.addAttribute("news", news);
return "MainLayout_Jay";//"home";
} else {
return "login";//"index";
}
}
我有两个问题。 1)最后一个表单提交dosent返回一个包含数据的视图。它只返回一个空页面,其中包含控制器方法的返回字符串。
2)我在tiles.xml中配置了我的主页,并将其放在/ webcontent / pages文件夹中。因此,如果瓷砖视图解析器无法找到它,则常规解析器应具有。我怀疑它是否将此请求视为ajax请求并完全跳过模型视图映射部分。我该如何找出根本原因。
2.5)如果我想解析ajax请求为Json,我应该使用@restcontroller并自动将响应发送为json吗?我尝试使用MappingJackson2HttpMessageConverter,但没有看到调试模式有任何区别?它甚至被使用了吗?
答案 0 :(得分:0)
知道了。它没有花太多时间来输入这个问题。
我需要在
删除@ResponseBody注释public @ResponseBody String resetPass(