Spring 4 对表单提交的响应未通过视图解析器。返回 return 字符串作为响应
Spring 4 response to form submission dosent go thru view resolver. Gives back return string in response
我在工作中继承了一个 spring 4 web 项目,该项目配置了 Tiles 视图框架、Spring 调度程序 servlet 和 IOC 框架。
我正在尝试构建一个重置密码功能,它具有三个屏幕和三种形式。
1)重置表格。获取用户的电子邮件和控制器方法发送带有临时密码的电子邮件。
<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>
2) 输入临时密码的表单:
<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>
3) 输入新密码的表单:
<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 提交并只取回数据,没有像下面这样的视图 jquery $.ajax()
$('#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();
});
第三种形式是常规形式提交,没有ajax提交。因为我希望用户在使用 temp 登录并设置新密码后被转发到他们的主页。
以下是前两种形式的控制器映射。
@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";
}
}
下面是我的 spring 配置
<bean id="annotationResolver" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.nexis.cardholder" />
<mvc:annotation-driven >
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="com.nexis.cardholder.session.interceptors.URLInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/Pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="1" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" >
<property name="order" value="2" />
</bean>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="0" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views.xml</value>
</list>
</property>
</bean>
我有两个问题。
1) 最后的表单提交发送 return 一个包含数据的视图。它只是 return 一个空页面,其中包含控制器方法的 return 字符串。
2) 我在 tiles.xml 中配置了我的主页,并将其放在 /webcontent/pages 文件夹中。所以如果 tiles view resolver 找不到它,常规解析器应该有。我怀疑它是否将此请求视为 ajax 请求并完全跳过模型视图映射部分。我如何找出根本原因。
2.5) 如果我想将 ajax 请求解析为 Json,我应该使用 @restcontroller 并且它会自动将响应发送为 json 吗?我尝试使用 MappingJackson2HttpMessageConverter 但在调试模式下没有发现任何区别??它甚至被使用了吗?
知道了。打这个问题花不了多少时间。
我需要删除
处的 @ResponseBody 注释
public @ResponseBody String resetPass(
我在工作中继承了一个 spring 4 web 项目,该项目配置了 Tiles 视图框架、Spring 调度程序 servlet 和 IOC 框架。
我正在尝试构建一个重置密码功能,它具有三个屏幕和三种形式。
1)重置表格。获取用户的电子邮件和控制器方法发送带有临时密码的电子邮件。
<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>
2) 输入临时密码的表单:
<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>
3) 输入新密码的表单:
<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 提交并只取回数据,没有像下面这样的视图 jquery $.ajax()
$('#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();
});
第三种形式是常规形式提交,没有ajax提交。因为我希望用户在使用 temp 登录并设置新密码后被转发到他们的主页。
以下是前两种形式的控制器映射。
@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";
}
}
下面是我的 spring 配置
<bean id="annotationResolver" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.nexis.cardholder" />
<mvc:annotation-driven >
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="com.nexis.cardholder.session.interceptors.URLInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/Pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="1" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" >
<property name="order" value="2" />
</bean>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="0" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views.xml</value>
</list>
</property>
</bean>
我有两个问题。 1) 最后的表单提交发送 return 一个包含数据的视图。它只是 return 一个空页面,其中包含控制器方法的 return 字符串。
2) 我在 tiles.xml 中配置了我的主页,并将其放在 /webcontent/pages 文件夹中。所以如果 tiles view resolver 找不到它,常规解析器应该有。我怀疑它是否将此请求视为 ajax 请求并完全跳过模型视图映射部分。我如何找出根本原因。
2.5) 如果我想将 ajax 请求解析为 Json,我应该使用 @restcontroller 并且它会自动将响应发送为 json 吗?我尝试使用 MappingJackson2HttpMessageConverter 但在调试模式下没有发现任何区别??它甚至被使用了吗?
知道了。打这个问题花不了多少时间。
我需要删除
处的 @ResponseBody 注释public @ResponseBody String resetPass(