3

我一直在浏览这里的帖子,尝试发布的所有内容,但我似乎无法让我的资源目录显示图像。

我想知道是否可以得到一些帮助,我的来源在下面列出。

web.xml

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>BoxRam Application</display-name>

    <!-- Spring MVC -->
    <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>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

mvc-调度程序-serverlet.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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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

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


    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

     <!-- Enables controllers mapped with @RequestMapping annotations, formatting annotations @NumberFormat @DateTimeFormat, and JSR 303 style validation -->  
    <mvc:annotation-driven/>  

    <mvc:resources mapping="/resources/**" location="/resources/" />  
    <mvc:default-servlet-handler />  
</beans>

弹簧安全.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    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.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd" >

    <http pattern="/resources/**" security="none" />
    <http pattern="/" security="none"/>

    <http pattern="/**" auto-config="false" use-expressions="true" access-denied-page="/loginfailed">
        <intercept-url pattern="/login/**" access="permitAll" />
        <intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" />
        <form-login login-page="/login" 
                    default-target-url="/welcome"
                    authentication-failure-url="/loginfailed"
                    always-use-default-target="true" />

        <logout invalidate-session="true" 
                delete-cookies="JSESSIONID" 
                logout-url="/logout" 
                logout-success-url="/" />
        <remember-me/>
        <session-management invalid-session-url="/login" >
            <concurrency-control max-sessions="2" />
        </session-management>

    </http>

    <global-method-security pre-post-annotations="enabled" />
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="test" password="00" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

登录控制器.java

package com.example.common.controller;

import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    @RequestMapping(value="/**", method = RequestMethod.GET)
    public String printDefault(ModelMap model) {

        return "index";

    }
    @RequestMapping(value="/welcome", method = RequestMethod.GET)
    public String printWelcome(ModelMap model, Principal principal ) {

        String name = principal.getName();
        model.addAttribute("username", name);
        model.addAttribute("message", "Spring Security Custom Form example");
        return "hello";

    }

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(ModelMap model) {

        return "login";

    }

    @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
    public String loginerror(ModelMap model) {

        model.addAttribute("error", "true");
        return "login";

    }

    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {

        return "login";

    }

}

索引.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
  Hello this is the Default Page.
<img src="<c:url value="/resources/imgs/logo.png" />"" />
</body>
</html>

应用程序中的目录结构

 - src
     * main
         * java
         * resources
         * webapp
             * WEB-INF
                 * pages
                 * resources
                      * css
                      * imgs
                          * logo.png
                      * js
                 * mvc-dispatch-servlet.xml
                 * spring-security.xml

任何帮助表示赞赏。

4

1 回答 1

6

好的,最后我发现了以下三个问题:

首先:mvc:resources声明缺少 /WEB-INF 部分:

  <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

二:在web.xml中去掉<servlet-mapping>for/resources路径

第三:在LoginController第一个@RequestMapping处理所有请求(/**)。这应该改为

@RequestMapping(value="/", method = RequestMethod.GET)
于 2013-06-01T21:40:59.093 回答