有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring添加mvc:resources标记

这是我的调度程序文件。我已经为schema/mvc、内容及其相应的位置添加了名称空间

现在,当我使用下面的xml运行我的应用程序时,我得到了我的应用程序找不到的页面,但是当我删除xmlns:mvc和resources标记时,它就可以正常工作了

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    ">


<context:component-scan base-package="com.school.controller" />

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
             <value>/WEB-INF/pages/</value>
          </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/static/" />

URL-http://localhost:8080/SchoolManagement/

网络。xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Servlet-

package com.school.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.school.beans.Login;

@Controller
public class Logincontroller {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView student() {
        return new ModelAndView("login", "loginform", new Login());
    }
}

共 (1) 个答案

  1. # 1 楼答案

    <mvc:resources mapping="/resources/**" location="/resources/static/" />
    

    该标记基本上表示,如果URI中的路径以“/resources/”开头,则返回位于“/resources/static/”中的文件

    检查您的文件(图片、css等)是否位于<your_webapp_root>/resources/static/

    编辑:

    我还注意到您的名称空间声明中使用了不同的版本:

    spring-mvc-3.0.xsd
    spring-beans-2.5.xsd
    spring-context-2.5.xsd
    

    检查依赖项并使用正确的版本