在Spring mvc中的表单发布后无法在视图中获取值

时间:2013-08-22 10:35:01

标签: java spring spring-mvc

这是我的ControllerClass

public class ScheduleClassController extends SimpleFormController {

public ScheduleClassController() {
    setCommandClass(ScheduleClass.class);
    setCommandName("scheduleClass");
}

protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command) throws Exception {

    ScheduleClass wiziqClass = (ScheduleClass) command;
    System.out.println(wiziqClass);
    return new ModelAndView("classdetail", "ScheduleClass", wiziqClass);

}
}

这是我的springapp-servelet.xml

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
    <property name="suffix" value=".jsp" />
    <property name="order" value="10" />
</bean>

<bean name="/index.htm" class="org.sakaiproject.wiziq.tool.HelloWorldController">
    <property name="sakaiProxy" ref="org.sakaiproject.wiziq.logic.SakaiProxy" />
</bean>

<bean name="/schedule.htm" class="org.sakaiproject.wiziq.tool.ScheduleClassController">
    <property name="formView" value="schedule" />
    <property name="successView" value="classdetail" />
</bean>

这是我的classdetail.jsp

<jsp:directive.include file="/templates/includes.jsp" />
<jsp:directive.include file="/templates/header.jsp" />

hello there
${wiziqClass.name}

<jsp:directive.include file="/templates/footer.jsp" />

提交表单后,我登陆此视图但未获取wiziqClass.name 我制作了模型并在那里添加了getter和setter。

我做错了什么?无法搞清楚。

3 个答案:

答案 0 :(得分:1)

请改变任何一个地方。不要在两个地方改变。

 protected ModelAndView onSubmit(HttpServletRequest request,
      HttpServletResponse response, Object command) throws Exception {

      ScheduleClass wiziqClass = (ScheduleClass) command;
      System.out.println(wiziqClass);
      return new ModelAndView("classdetail", "wiziqClass", wiziqClass);
}

 ${ScheduleClass.name}

答案 1 :(得分:0)

根据你的代码,属性的名称是'ScheduleClass'而不是'wiziqClass'...所以改变jsp中的表达式

protected ModelAndView onSubmit(HttpServletRequest request,
    HttpServletResponse response, Object command) throws Exception {

  ScheduleClass wiziqClass = (ScheduleClass) command;
  System.out.println(wiziqClass);
  return new ModelAndView("classdetail", "ScheduleClass", wiziqClass);
}

$ {ScheduleClass.name}

答案 2 :(得分:0)

让它工作主要问题是onSubmit无法正常工作我错误地删除了 来自B的异常错误   onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,BindException errors)  那导致错误的第二个错误是M.Dienum所说的

感谢你们俩