我在我的项目中使用 JstlView Spring Tiles。
当我尝试通过控制器发送表单:从验证器到 jsp 的错误时,jsp 中不会显示错误
当我调试时,
1) 在从验证器类返回 modelandview 之前,控制器类中会打印错误。
2)然后错误也从控制器类打印到 JstlView 类中。
所以我认为,jstlview 类中的 requestDispatcher.forward(request, response) 不会将模型数据返回给 jsp。
因为当我尝试使用 HttpServletRequest 或 HttpServletResponse 对象来检索 jsp 中的错误时,jsp 中会显示值。
有人可以帮助我使用这个。
如何将模型数据发送到 jsp renderMergedOutputModel- RequestDispatcher ,重定向方法。
我的代码如下,
JstlView 类:
public class JstlView extends InternalResourceView {
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// set original view being asked for as a request parameter
request.setAttribute("partial", dispatcherPath.substring(dispatcherPath.lastIndexOf("/") + 1));
// force everything to be template.jsp
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/template.jsp");
requestDispatcher.forward(request, response);
System.out.println("**********************"+model.get("userName"));
}
}
小服务程序.xml
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="com.tms.web.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
控制器:
private LoginValidator loginValidator;
@Autowired
public void setUserValidator(LoginValidator loginValidator) {
this.loginValidator = loginValidator;
}
final RequestHandler requestHandler = new RequestHandler();
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
return new ModelAndView("login", "users", new users());
}
@RequestMapping(value = "/logins",method = RequestMethod.POST)
public ModelAndView validateUser(@Valid users user, BindingResult result,@ModelAttribute("users")users users,ModelMap model,RedirectAttributes redirectAttributes,HttpServletRequest req)
{
this.loginValidator.validate(user, result);
if (result.hasErrors())
{
model.putAll(result.getModel());//values not retuned to jsp
req.setAttribute("userName", result.getFieldError().getDefaultMessage().toString()); //working fine values retuned to jsp
return new ModelAndView("/login", model);
}
else
{
//succes related code
return new ModelAndView(redirect, model);
}
}
验证器
@Component
public class LoginValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return clazz.isAssignableFrom(users.class);
}
@Override
public void validate(Object obj, Errors errors) {
users user = (users) obj;
String userName = user.getUserName();
String password = user.getPassword();
validateuserName(userName,password, errors);
}
private void validateuserName(String userName, String password,Errors errors) {
if (!(isValidString(userName))) {
errors.rejectValue("userName", "userName.required","Username should not be blank");
}
else if(!(isValidString(password)))
{
errors.rejectValue("password", "password.required","Password should not be blank");
}
}
private boolean isValidString(String str) {
return isNotNull(str) && (str.length() > 0 ) && !(str.isEmpty());
}
private boolean isNotNull(String str) {
return str != null;
}
}
jsp
<%@ page language="java" contentType="text/html;charset=UTF-8; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ page isScriptingEnabled="true" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<form:form method="POST" action="/TMSWeb/logins" commandName="users" modelAttribute="users">
<input name="password" type="password" align="center"/>
<form:errors path="password" cssClass="error" element="div" />
<input type="submit" class="buttonorange" value="Login"/>
</form:form>
在 Spring MVC 中,当我可以在没有 JstlView 的情况下访问 JSP 中的 bean 时,我可以在我的 JSP 中编写 (${errors)。但是当同一个 JSP 是瓦片视图的一部分时,这些属性就无法访问了。
有人可以在这个问题上帮助我吗?