【问题标题】:request mapping in spring mvc error occued:requested resource not availablespring mvc中的请求映射错误发生:请求的资源不可用
【发布时间】:2018-03-06 02:52:45
【问题描述】:

我在我的 spring mvc 项目中有一个部分,我必须从网站下载一个文件。我将控制器方法写为:

//download resume
//----------------------------------------------------------------------  
 @RequestMapping(value="/download/{fileName.+}",method=RequestMethod.GET)
public void downloadPDFResource(HttpServletRequest request,
        HttpServletResponse response,@PathVariable("fileName") String fileName){


     System.out.println("filename i got :"+fileName);
    //If user is not authorized - he should be thrown out from here itself

    //String fileName="Ente Kadha - Madhavikkutty.pdf";

    //Authorized user will download the file
    String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/downloads/");
    Path file = Paths.get(dataDirectory, fileName);
    if (Files.exists(file)) {
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        try {
            Files.copy(file, response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    else
    {
        System.out.println("\n\nFile not found!!\n\n");
        System.out.println(file);
    }
}

请求下载文件的查看页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Profile</title>
    </head>
    <body>

        <table>

            <th><h3>${profile.name}</h3></th>
        <tr>
            <td>Application Id :</td>
            <td>${profile.a_id}</td>
        </tr>
        <tr>
            <td>Name :</td>
            <td>${profile.name}</td>
        </tr>
        <tr>
            <td>Address :</td>
            <td>${profile.address}</td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>${profile.email}</td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td>${profile.phone}</td>
        </tr>
        <tr>
            <td>Vacancy id:</td>
            <td></td>
        </tr>
        <tr>
            <td>Date Applied :</td>
            <td>${profile.dateApplied}</td>
        </tr>
        <tr>
             <td>Resume :  ${profile.resumePath}${profile.resumeDoc} </td>
             <td><a href="download/${profile.resumeDoc}">Download ${profile.resumeDoc}</a></td> 
    </tr>

</table>

</body>
</html>

我的调度器-servlet:

 <?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
       xmlns:p="http://www.springframework.org/schema/p"    
       xmlns:context="http://www.springframework.org/schema/context"    
       xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="controller"></context:component-scan>
    <context:component-scan base-package="DAO"></context:component-scan>

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

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <!--<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>-->

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean id="multipartResolver"   
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <bean id="con" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="//my url"></property>
        <property name="username" value="root"></property>   
        <property name="password" value="root"></property> 

        <!--
        //for ms sql server,it may be like :
        database-driver=net.sourceforge.jtds.jdbc.Driver
        url=jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS
        username=shoppingcart
        password=12345
        -->     
    </bean>

    <!--    normal jdbc template bean-->
    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="con"></property>
    </bean>


    <!--  this bean uses normal jdbc template -->
    <bean id="contactsDAO" class="DAO.ContactsDAO"> 
        <property name="jdbc" ref="template"></property>
    </bean>

    <!--  this bean uses normal jdbc template -->
    <bean id="vacancyDAO" class="DAO.VacancyDAO"> 
        <property name="jdbc1" ref="template"></property>
    </bean>

    <!--  this bean uses normal jdbc template -->
    <bean id="careerDAO" class="DAO.CareerDAO"> 
        <property name="jdbc2" ref="template"></property>
    </bean>

</beans>

web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

单击下载链接时,我收到错误消息:“请求的资源不可用”:

WARNING [http-nio-8084-exec-129] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/IntelliLabsWeb/oneProfile/download/Ente Kadha - Madhavikkutty.pdf] in DispatcherServlet with name 'dispatcher'

我猜请求的映射有错误。当我将 web.xml 中的 url 模式从 / 更改为 /* 时,所有其他请求都不起作用。

我的问题是,我在这里犯了什么错误?我应该更改 web.xml 中的 url 模式吗?如果是,怎么做?

编辑:

当我点击下载链接时,它会转到一个以 .pdf 结尾的 url 延期。并且请求没有处理程序或映射 这样的扩展。这会是个问题吗??

【问题讨论】:

    标签: spring model-view-controller request mapping


    【解决方案1】:

    我认为您可以尝试以下 2 个步骤来完成此操作。

    1) 更改请求映射(文件名后面有个小错别字,去掉)

    @RequestMapping(value="/download/{fileName}",method=RequestMethod.GET)
    

    2) 将链接更改为(带有根斜线)

    <a href="/download/${profile.resumeDoc}">Download ${profile.resumeDoc}</a>
    

    否则,路径名将从应用程序上下文中派生。希望你有这个页面

    http:<server url>/IntelliLabsWeb/oneProfile
    

    web.xml 配置应该是这样的

    &lt;url-pattern&gt;/&lt;/url-pattern&gt;

    希望这能解决您的问题。

    【讨论】:

    • 你能解释一下吗:“希望你在 http:/IntelliLabsWeb/oneProfile 上有这个页面”
    • 没有工作是还是同样的错误还是不同的错误?
    【解决方案2】:

    我是 Spring 框架的新手。 我认为你应该修改 web.xml:

    <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.*</url-pattern> </servlet-mapping>

    希望能有所帮助!

    【讨论】:

    • 为什么说url格式要按照上面说的修改??实际上,如果没有 url 模式,我无法访问任何控制器:/
    • 因为 *.* 可以映射调度程序 servlet 的所有请求
    猜你喜欢
    • 1970-01-01
    • 2017-08-06
    • 2013-02-16
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多