spring-mvc:表单控制器的错误映射URL

时间:2010-01-18 09:15:06

标签: spring-mvc

我对spring mvc有问题

我的春豆     

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="welcome.htm">welcomeController</prop>
        </props>
    </property>
</bean>

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

<bean name="welcomeController"
    class="org.springframework.web.servlet.mvc.ParameterizableViewController"
    p:viewName="welcome" />

<bean name="/bscList.htm" class="cbs.web.BscController">
    <property name="bscDao" ref="myBscDao" />
</bean>

<bean name="/bscForm.htm" class="cbs.web.BscFormController">
    <property name="commandName" value="bsc"/>
    <property name="commandClass" value="cbs.domain.Bsc"/>
    <property name="formView" value="bscForm"/>
    <property name="successView" value="bscList.htm"/>
    <property name="bscDao" ref="myBscDao"/>
</bean>

</beans>

我的表单控制器

public class BscFormController extends SimpleFormController {

private static Logger log = Logger.getLogger(BscController.class);
private BscDao bscDao;

public void setBscDao(BscDao bscDao) {
    this.bscDao = bscDao;
}

protected Object formBackingObject(HttpServletRequest request)
        throws Exception {
    String id = request.getParameter("id");

    if (!StringUtils.isBlank(id)) {
        return bscDao.get(new Integer(id));
    }

    return new Bsc();
}

public ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors)
        throws Exception {
    log.debug("entering 'onSubmit' method...");

    Bsc bsc = (Bsc) command;
    String success = getSuccessView();

    if (request.getParameter("delete") != null) {
        bscDao.remove(bsc.getId());
    } else {
        bscDao.save(bsc);
    }

    return new ModelAndView(success);
}
}

我的问题: 当我访问/bscList.htm时,它是bsc(bscList.jsp模板)的显示列表, 但是当我访问/bscForm.htm时,它仍然显示bsc的列表,而不是显示我的表单(bscForm.jsp模板)

我用一些简单的控制器进行测试:

  • 控制器实现org.springframework.web.servlet.mvc.Controller,evething正常运行
  • controller扩展SimpleFormController,映射错误: 在DispatcherServlet中找不到带有URI [/cbs/testform.htm]的HTTP请求的映射,名称为“dispatcher”

1 个答案:

答案 0 :(得分:0)

当我使用HandlerMapping ControllerClassNameHandlerMapping时,所有请求URL'/ bsc *'将映射到BscController(/bscForm.htm将不会映射到BscFormController)