javascript - 使用 MVC 标记添加 css、js 时,找不到使用 URI Spring MVC 的 HTTP 请求的映射

标签 javascript java css spring jsp

这是我的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>XYZ_POC</display-name>

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

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

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

和我的 dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        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.xsd">

    <context:component-scan base-package="com.abc.pqr" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/safePages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

和我的 Controller 类

@Controller
public class MainController {

    private static final String VIEW_INDEX = "index";

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        User requestForm = new User();
        model.addAttribute("requestForm",requestForm);
        return VIEW_INDEX;
    }
}

这很好用。

我的 jsp 文件中有一些 CSS 和 JS 代码,我想将它们与 JSP 分开。

我关注了这个tutorial逐字地去做。

所以我的新调度程序 servlet xml 变成了:

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

    <context:component-scan base-package="com.abc.pqr" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/safePages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <mvc:resources mapping="/resources/theme/**" location="/resources/theme/"/>
    <mvc:resources mapping="/resources/jscript/**" location="/resources/jscript/"/>

</beans>

但是当我部署它时,我的应用程序停止工作。从本地主机访问上下文根时,我开始收到“未找到 HTTP 请求的映射...”错误。

我错过了什么?

最佳答案

我遇到了同样的问题。在 spring conf 中添加以下标签对我有用

"mvc:default-servlet-handler/>"

关于javascript - 使用 MVC 标记添加 css、js 时,找不到使用 URI Spring MVC 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36978437/

相关文章:

javascript - 保护 Electron 应用程序的最佳实践

javascript - 如何让我的 div 以水平方式显示?

java - 有效地测试数组成员资格

java - 如何在 Spring JMS 监听器中从 JMS 消息获取自定义属性

javascript - 将类添加到滚动页面jquery上的div

javascript - 找不到模块 : Error: Can't resolve 'avoriaz'

java - 针对 Jena 数据集默认图的 SPARQL 查询

html - CSS 规则如何覆盖另一个 CSS 规则?

html - 网站在 Windows 和 Mac 上定位元素的方式不同

javascript - 另一个元素后面的 Canvas 上的事件监听器