相关疑难解决方法(0)

如何在java应用程序上下文中使用spring MVC的<mvc:resources>标记?

我已经创建了"for now"一个简单而基本的Spring Web应用程序.我习惯将部署描述符作为简单的web.xml文件,然后将应用程序上下文作为xml文件.

虽然,现在我想尝试仅使用java文件创建我的整个spring Web应用程序.因此,我创建了我的WebApplicationInitializer,而不是正常的部署描述符,以及使用@Configuration批注的应用程序上下文.

部署描述符

package dk.chakula.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
 *
 * @author martin
 * @since 12-1-2012
 * @version 1.0
 */
public class Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        registerDispatcherServlet(servletContext);
    }

    private void registerDispatcherServlet(final ServletContext servletContext) {
        WebApplicationContext dispatcherContext = createContext(ChakulaWebConfigurationContext.class);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
        Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

    private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
        AnnotationConfigWebApplicationContext …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc

15
推荐指数
2
解决办法
2万
查看次数

spring mvc如何绕过DispatcherServlet获取*.html文件?

web.xml片段

<!-- Handles all requests into the application -->
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/app-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我不想让Dispatcher Servlet处理*.html请求.我如何做到这一点?谢谢.

web.xml servlets spring-mvc

13
推荐指数
2
解决办法
2万
查看次数

如何阻止Spring MVC阻塞所有其他Servlet?

我正在使用Spring 2.5 MVC并希望添加另一个第三方Servlet.问题是,Spring MVC捕获所有请求,因此Servlet没有得到任何请求.这是一个web.xml片段:

SpringMVC org.springframework.web.servlet.DispatcherServlet 2

<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>  

<servlet>
    <description>This is the servlet needed for cache.type servlet, returns the packed resources</description>
    <display-name>PackServlet</display-name>
    <servlet-name>PackServlet</servlet-name>
   <servlet-class>net.sf.packtag.servlet.PackServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>PackServlet</servlet-name>
    <url-pattern>*.pack</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

应用程序确实需要/*映射,pack:标签(第三方Servlet)确实需要基于文件扩展名的映射.告诉Spring不要处理请求的任何可能性?感谢致敬.

java spring servlets spring-mvc

9
推荐指数
1
解决办法
2926
查看次数

root("/")上的spring mvc网站

我想将spring mvc controller映射到root(/**)路径(而不是像"/ something"这样的子文件夹),同时使用mvc:resources(打开另一个方法)进行异常.

这应该是该框架的基础知识,但显然是一个非常复杂的问题.

app-servlet.xml有这些明显的映射异常:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />
Run Code Online (Sandbox Code Playgroud)

我有这个控制器:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

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

@Controller
@RequestMapping("/**")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String service(final HttpServletRequest request) {
        final String servlet_path = request.getServletPath();
        System.out.println(String.format("%s %s", new Date().toString(), servlet_path));
        return "test";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我点击"/"或"/ test"或"/ test/page"时,我得到如下输出:

Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico
Run Code Online (Sandbox Code Playgroud)

..看?service()正在被要求, …

java mapping servlets spring-mvc

7
推荐指数
2
解决办法
1万
查看次数

Spring使用mvc:resources提供静态内容,xsd无效

根据http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources的建议,我希望用它<mvc:resources>来提供我的弹簧静态内容.

我尝试了以下XML,但.xsd文件不包含声明<mvc:resources>,我找不到替代.xsd.我可以忽略eclipse错误,但由于SAXParseException,服务器无法启动.

我哪里出错了?

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

</beans>
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc

4
推荐指数
1
解决办法
1万
查看次数

Spring 3.1-找不到javascript文件 - 404错误

我在文件夹WebContent/resources/js/test.js中有一个js文件.我试图在jsp中包含相同的文件.但是jsp文件无法找到js文件(浏览器控制台中出现404错误).我在SO中抛出了几个问题:

可以将SpringMVC配置为处理所有请求,但排除静态内容目录吗?

在调用转发到JSP的Servlet时,浏览器无法访问/查找CSS,图像和链接等相关资源

STS Spring MVC:如何在JSP中包含JS文件

但仍然没有帮助.这是我的代码:

在应用程序上下文中,我使用的是mvc:resource标记.

<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)

在我的jsp中

<script src="${contextPath}/resources/js/test.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

尝试给src值

 <c:url> 
Run Code Online (Sandbox Code Playgroud)

太.

我的web.xml有

<servlet-mapping> 
<servlet-name>TestProject</servlet-name> 
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

萤火虫说

The requested resource (/resources/js/test.js) is not available.
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

UPDATE

firebug中的GET请求URL就是这个

http://localhost:8080/TestProject/resources/js/test.js
Run Code Online (Sandbox Code Playgroud)

这样对吗??

javascript spring jsp spring-mvc

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

spring-mvc ×6

java ×3

servlets ×3

spring ×3

javascript ×1

jsp ×1

mapping ×1

web.xml ×1