在MVC中找不到404资源

时间:2018-01-08 20:38:09

标签: java xml spring spring-mvc

使用spring在MVC中找不到404资源 任何文件都没有错误,代码也正确仍然出错。我多次检查过。 下面是我的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

任何文件都没有错误,代码也正确仍然出错。我多次检查过。 下面是我的web.xml,* -servlet.xml和我的beans.xml文件

*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,* -servlet.xml和我的beans.xml文件

*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>

1 个答案:

答案 0 :(得分:1)

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

解决方案:

1.您可以将jsps移动到WebContent目录 2.在welcome-file标记中,给出index.jsp的完整路径 示例:<welcome-file>/jsps/index.jsp</welcome-file>
3.由于您使用Spring和InternalResourceViewResolver,您还可以配置控制器以加载欢迎页面而不是移动文件。 示例:

@RequestMapping(value="Login.jsp")
    public ModelAndView showLoginPage(HttpServletRequest request, HttpServletResponse response ){
        return new ModelAndView("Login");
    }

然后使用网址访问该网页: http://localhost:port/context_root_of_your_application/Login.jsp

Dispatcher Servlet将选择此调用 - 重定向到控制器 - 在/ jsps /中查找视图(“Login”),如果找到匹配的jsp,它将重定向到该jsp。

希望这有帮助