开发者

Spring SimpleUrlHandlerMapping not allowing routing for any html page

I'm trying to implement a file upload in my Spring application based on the Spring documentation. However, when I add the SimpleUrlHandlerMapping reference, I can't even route to my login page.

In my web.xml, I have all .htm files mapped to my servlet:

<servlet-mapping>
  <servlet-name>myapp</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

then, in myapp-servlet.xml, I have this:

<bean name="/login.htm" class="com.example.web.LoginFormController">
  <property name="sessionForm" value="true" />
  <property name="commandName" value="loginCommand" />
  <property name="commandClass" value="com.example.command.LoginCommand" />
  <property name="authenticationService" ref="authenticationService" />
  <property name="loginDataAccess" ref="loginDAO" />
  <property name="validator" ref="loginValidator" />
  <property name="formView" value="login" />
  <property name="successView" value="hello.htm" />
</bean>

This allows me to go to the login page and login. However, when I add the following based on the documentation, I get this error: No mapping found for HTTP request with URI [/myapp/login.htm] in DispatcherServlet with name 'myapp'

These are the lines I added to the myapp-servlet.xml file:

<bean id="multipartResolver" c开发者_Go百科lass="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <value>/upload.form=fileUploadController</value>
  </property>
</bean>

<bean id="fileUploadController" class="com.example.web.FileUploadController">
  <property name="commandClass" value="com.example.domain.FileUploadBean" />
  <property name="formView" value="fileuploadform" />
  <property name="successView" value="hello.htm" />
</bean>

If I take out the urlMapping section, I can navigate to the upload page, but then I get an IllegalStateException. How do I have Spring route the htm files normally and allow me to upload a file? Shouldn't the urlMapping only matter when the upload.form is called?

Update

Per SKaffman's answer, I updated the myapp-servlet.xml to look like this:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <value>
    /upload.form=fileUploadController
    /upload.htm=fileUploadController
    /login.htm=loginController
    </value>
  </property>
</bean>

This allows me to navigate to the upload page, but when I click on the submit button, I get a 404 for upload.form saying "(/myapp/upload.form) is not available." If I take the upload.htm out of the mapping, I can't navigate to that page. I don't know what mapping to use for both the page and the upload. The upload page has this action defined:

<form method="post" action="upload.form" enctype="multipart/form-data">


By default, Spring registers multiple HandlerMapping beans automatically, including a BeanNameUrlHandlerMapping. This handler allows you to use things like <bean name="/login.htm"> without any additional configuration.

However, as soon as you add an explicit handler mapping bean, like your SimpleUrlHandlerMapping, then the default ones are no longer registered, and your login controller will not longer be mapped.

The simplest solution for you is probably to add your login controller to the SimpleUrlMappingHandler:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <value>/login.htm=loginController</value>
    <value>/upload.form=fileUploadController</value>
  </property>
</bean>

<bean id=loginController" class="com.example.web.LoginFormController">
   ...
</bean>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜