Spring MVC没有显示jsp的结果

时间:2011-09-16 15:44:59

标签: spring-mvc

我是Spring Framework的新手。在我的POC中,我没有在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页面中返回命令对象详细信息,所以我写如下(thanks.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}正在打印,因为它是为什么?

2 个答案:

答案 0 :(得分:0)

尝试启用EL     &lt;%@ page isELIgnored =“false”%&gt;

答案 1 :(得分:0)

确保您在页面上有taglib定义: %@ taglib prefix =“c”uri =“http://java.sun.com/jsp/jstl/core”%&gt;