Java 404在MVC中找不到资源

Java 404在MVC中找不到资源,java,xml,spring,spring-mvc,Java,Xml,Spring,Spring Mvc,404使用spring在MVC中找不到资源 任何文件中都没有错误,代码也正确,但仍有错误。我检查了很多次。 下面是我的web.xml、*-servlet.xml和beans.xml文件 *Beans.XML* <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/

404使用spring在MVC中找不到资源 任何文件中都没有错误,代码也正确,但仍有错误。我检查了很多次。 下面是我的web.xml、*-servlet.xml和beans.xml文件

*Beans.XML*
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <bean id="product" class="com.shoppingcart.beans.Product">

        <property name="id" value="10"></property>
        <property name="name" value="Apple"></property>

    </bean>

    <bean name="vendor" class="com.shoppingcart.beans.Vendor">
        <property name="name" value="iStore"></property>
        <property name="city" value="Thane"></property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
    </bean>

    <bean name="account" class="com.shoppingcart.model.Account"></bean>

    <context:component-scan
        base-package="com.shoppingcart.beans, com.shoppingcart.model, com.shoppingcart.controller">
    </context:component-scan>
    <context:annotation-config></context:annotation-config>
    <context:property-placeholder
        location="com/shoppingcart/main/jdbc.properties" />


</beans>


----------------------------------------------------------------------------
404-resource not found in MVC using spring
*shoppingcart-servlet.xml*

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <context:component-scan base-package="com.shoppingcart.controller">
    </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

----------------------------------------------------------------------------404-resource not found in MVC using spring
*web.xml*

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ShoppingCart</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>shoppingcart</display-name>
    <servlet-name>shoppingcart</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>shoppingcart</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
*Beans.XML*
----------------------------------------------------------------------------
404使用spring在MVC中找不到资源
任何文件中都没有错误,代码也正确,但仍有错误。我检查了很多次。 下面是我的web.xml、*-servlet.xml和beans.xml文件

*Beans.XML*
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <bean id="product" class="com.shoppingcart.beans.Product">

        <property name="id" value="10"></property>
        <property name="name" value="Apple"></property>

    </bean>

    <bean name="vendor" class="com.shoppingcart.beans.Vendor">
        <property name="name" value="iStore"></property>
        <property name="city" value="Thane"></property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
    </bean>

    <bean name="account" class="com.shoppingcart.model.Account"></bean>

    <context:component-scan
        base-package="com.shoppingcart.beans, com.shoppingcart.model, com.shoppingcart.controller">
    </context:component-scan>
    <context:annotation-config></context:annotation-config>
    <context:property-placeholder
        location="com/shoppingcart/main/jdbc.properties" />


</beans>


----------------------------------------------------------------------------
404-resource not found in MVC using spring
*shoppingcart-servlet.xml*

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <context:component-scan base-package="com.shoppingcart.controller">
    </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

----------------------------------------------------------------------------404-resource not found in MVC using spring
*web.xml*

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ShoppingCart</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>shoppingcart</display-name>
    <servlet-name>shoppingcart</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>shoppingcart</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
*shoppingcart-servlet.xml*
----------------------------------------------------------------------------404使用spring在MVC中找不到资源
任何文件中都没有错误,代码也正确,但仍有错误。我检查了很多次。 下面是我的web.xml、*-servlet.xml和beans.xml文件

*Beans.XML*
<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <bean id="product" class="com.shoppingcart.beans.Product">

        <property name="id" value="10"></property>
        <property name="name" value="Apple"></property>

    </bean>

    <bean name="vendor" class="com.shoppingcart.beans.Vendor">
        <property name="name" value="iStore"></property>
        <property name="city" value="Thane"></property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
    </bean>

    <bean name="account" class="com.shoppingcart.model.Account"></bean>

    <context:component-scan
        base-package="com.shoppingcart.beans, com.shoppingcart.model, com.shoppingcart.controller">
    </context:component-scan>
    <context:annotation-config></context:annotation-config>
    <context:property-placeholder
        location="com/shoppingcart/main/jdbc.properties" />


</beans>


----------------------------------------------------------------------------
404-resource not found in MVC using spring
*shoppingcart-servlet.xml*

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <context:component-scan base-package="com.shoppingcart.controller">
    </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

----------------------------------------------------------------------------404-resource not found in MVC using spring
*web.xml*

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ShoppingCart</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>shoppingcart</display-name>
    <servlet-name>shoppingcart</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>shoppingcart</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
*web.xml*
购物卡
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
购物车
购物车
org.springframework.web.servlet.DispatcherServlet
购物车
/

索引在哪里?
欢迎文件中提到的文件放置在哪里?
当应用程序启动时,它正在检查
WebContent
文件夹中提到的
web.xml
欢迎文件。但是如果您将它们放在
WebContent/jsps/
中,将找不到请求的文件。这可能会导致404

解决方案:

1.您可以将JSP移动到
WebContent
目录。
2.在
欢迎文件
标记中,给出index.jsp的完整路径
示例:
/jsps/index.jsp

3.由于您使用的是Spring和
InternalResourceViewResolver
,因此还可以配置控制器来加载欢迎页面,而不是移动文件。 例如:

@RequestMapping(value="Login.jsp")
    public ModelAndView showLoginPage(HttpServletRequest request, HttpServletResponse response ){
        return new ModelAndView("Login");
    }
然后使用url访问页面:

DispatcherServlet将选择这个调用——重定向到控制器——在/jsps/中查找视图(“Login”),如果找到匹配的jsp,它将重定向到该jsp


希望这有帮助

你在学习一些古老的教程吗?不!!!!你为什么这么问?看起来不太现代。但是你是否发现任何逻辑错误会导致404资源找不到例外你可以发布你的被调用的url、servlet和war结构吗?