如何在Spring MVC中浏览htm文件

发布于 2024-12-27 04:10:52 字数 240 浏览 1 评论 0原文

我正在使用 Spring MVC 2 的默认配置。因此,我的调度程序看起来像,

 <prop key="index.htm">indexController</prop>

我在根目录下有一个简单的 htm 文件。当我浏览此文件时,出现 404:Not Found 错误。如何在 Spring MVC 中浏览(物理存在于磁盘上)htm 文件。

I am using default configuration of Spring MVC 2. So, my dispatcher will look like,

 <prop key="index.htm">indexController</prop>

I have a simple htm file on root. When I browse this file I get 404:Not Found Error. How to browse (which exist physically on disk) htm file in Spring MVC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゞ花落谁相伴 2025-01-03 04:10:52

在基于注释的 Spring 3.x 中,您可以将其写在控制器中,

@RequestMaping(value="/index.htm")
public void doSomeJob(){
    //some code here
}

如果您以“/pathToIt/index.htm”形式发出请求,那么它将被 doSomeJob() 方法捕获。

PS 不需要Spring MVC 3.x 中配置文件中的请求映射

注意:如果 spring 找不到您的物理文件,也可能导致 404。

In annotation based Spring 3.x, you can just write it in your controller as,

@RequestMaping(value="/index.htm")
public void doSomeJob(){
    //some code here
}

and if you make a request as "/pathToIt/index.htm", then it will be caught by doSomeJob() method..

P.S No need for request mappings in configuration files in Spring MVC 3.x

Note: And also 404 can be caused if spring can't find your physical file..

变身佩奇 2025-01-03 04:10:52

尝试在 springmvc-servlet.xml 文件中添加一个 bean id:

<bean id="indexController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

然后:

   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
               ...
           <prop key="/index.htm">indexController</prop>
         </props>
       </property>
    </bean>

Try and add a bean id in your springmvc-servlet.xml file :

<bean id="indexController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

then :

   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
               ...
           <prop key="/index.htm">indexController</prop>
         </props>
       </property>
    </bean>
青春有你 2025-01-03 04:10:52

好吧,这听起来很熟悉,如果您使用的是 MVC 2,那么实现此目的的最佳方法是为调度程序 servlet 提供特定的映射,而不是 /

    <servlet>
      <servlet-name>myDispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myDispatcherServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

这将确保对 *.jsp 的唯一请求将进入调度程序服务,其余请求将由容器本身处理。

Ok this sounds familiar , if you are using MVC 2 then the best way to achieve this is to give a specific mapping for dispatcher servlet instead of /

    <servlet>
      <servlet-name>myDispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myDispatcherServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

This will make sure the only requests to *.jsp will go dispatcher servet and rest will be handled by the container itself.

温暖的光 2025-01-03 04:10:52

您需要将 URL index.htm 与您的控制器进行映射。在页面具有正确的 spring mvc 配置之前,您无法查看页面。

<bean name="/index.htm" class="com.indexController">
 ....
</bean>

在控制器中,您将在 modelAndView 中将 jsp 页面名称作为 view 传递。

检查您的视图解析器是否有正确的设置,如下所示

<!-- View Resolver -->
<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/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

You need to map URL index.htm with your controller. You cannot view your page until it will have correct spring mvc configuration.

<bean name="/index.htm" class="com.indexController">
 ....
</bean>

In controller you will pass your jsp page name as view in modelAndView.

Check your view resolver has correct setting like this

<!-- View Resolver -->
<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/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文