2011-09-16 40 views
0

我是Spring Framework的新成员。在我的POC中,我没有得到jsp中的模型值。下面 是我的代码 我的控制器是Spring MVC未在jsp中显示结果

@Controller 
public final class ContactController { 

    @Autowired 
    private Validator validator; 

    public void setValidator(Validator validator) { 
     this.validator = validator; 
    } 

    @RequestMapping(value = "/form", method = RequestMethod.GET) 
    public String get(ModelMap model) { 

     // Because we're not specifying a logical view name, the 
     // DispatcherServlet's DefaultRequestToViewNameTranslator kicks in. 
     UserMessage Message = new UserMessage(); 
     System.out.println("Hello Get Method"); 
     model.addAttribute("userMessage", Message); 
     return "form"; 
    } 

    @RequestMapping(value = "/form", method = RequestMethod.POST) 
    public String post(@ModelAttribute("userMessage") UserMessage userMsg, 
      BindingResult result, Model model) { 

     validator.validate(userMsg, result); 
     System.out.println(userMsg.getName()); 
     model.addAttribute("userMsg",userMsg); 
     if (result.hasErrors()) { return "form"; } 

     // Use the redirect-after-post pattern to reduce double-submits. 
     return "thanks"; 
    } 

我的JSP形式如下

<form:form modelAttribute="userMessage"> 
    <div class="form-item"> 
     <div class="form-label">Your name:</div> 
     <form:input path="name" size="40" cssErrorClass="form-error-field"/> 
     <div class="form-error-message"><form:errors path="name"/></div> 
    </div> 
    <div class="form-item"> 
     <div class="form-label">Your e-mail address:</div> 
     <form:input path="email" size="40" cssErrorClass="form-error-field"/> 
     <div class="form-error-message"><form:errors path="email"/></div> 
    </div> 
    <div class="form-item"> 
     <div class="form-label">Your message:</div> 
     <form:textarea path="text" rows="12" cols="60" cssErrorClass="form-error-field"/> 
     <div class="form-error-message"><form:errors path="text"/></div> 
    </div> 
    <div class="form-item"> 
     <input type="submit" value="Submit" /> 
    </div> 
</form:form> 

配置文件,如下

<bean id="configurationLoader" 
     class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader"/> 

    <bean id="validator" class="org.springmodules.validation.bean.BeanValidator" 
     p:configurationLoader-ref="configurationLoader"/> 

    <!-- Load messages --> 
    <bean id="messageSource" 
     class="org.springframework.context.support.ResourceBundleMessageSource" 
     p:basenames="errors"/> 

    <!-- Discover POJO @Components --> 
    <!-- These automatically register an AutowiredAnnotationBeanPostProcessor --> 
    <context:component-scan base-package="contact"/> 

    <!-- Map logical view names to physical views --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp"/> 

填充我想要的命令对象细节的表格后,回到jsp页面,所以我写如下(谢谢.jsp)

<%@ page import="contact.UserMessage" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Thank You</title> 
    </head> 
    <body> 
     <h1>Thank You</h1> 
     Welcome <%= request.getParameter("name") %> 

     Name is ${userMsg.name} 
      </body> 
</html> 

request.getParameter(“name”)给出了正确的结果,但$ {userMsg.name}正在打印,因为它是为什么?

+0

请任何人都可以帮助我 –

回答

0

尝试启用EL <%@页isELIgnored = “假” %>

0

请确保您有在页面上的taglib定义: %@标签库前缀= “C” URI =“HTTP: //java.sun.com/jsp/jstl/core“%>