图像不显示在spring-mvc中

时间:2017-04-24 09:56:25

标签: java jsp spring-mvc

我正在尝试在spring-mvc app中的index.jsp上显示图像但是无法找到图像的实际路径我添加了 mvc:resources 标记的正确路径但是静止图像是没有显示。这是我的代码。

弹簧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" xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="in.net.usit"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/lts2d/lts2dnew"></property>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="in.net.usit.beans"></property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql"> true</prop>
                <prop key="hibernate.hb2ddl.auto">update</prop>
                <prop key="hibernate.connection.pool_size">1</prop>
            </props>
        </property>
    </bean>
    <bean  id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/img/"/>
    <!-- <mvc:default-servlet-handler /> -->

&#13;
&#13;
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script
	src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
	
<script
	src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-resource.min.js"></script>
<script type="text/javascript">
	var module = angular.module('myApp', []);
	module.controller('myController', function($scope) {
		console.log('inside the index page');
	});
</script>



<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body >
	<%-- <img src="<c:url>/img/ImgPitSide.png"</c:url> --%>
	<img src="<c:url value='/img/ImgPitSide.png'/>"/>
	</body>
</html>
&#13;
&#13;
&#13;

indexController.java

package in.net.usit.controller;

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

import in.net.usit.service.LadleLocationServiceImpl;

@Controller
public class IndexController {

  private LadleLocationServiceImpl ladleLocationService;

 @RequestMapping(value="/main",method=RequestMethod.GET)
 public String getIndexPage(){
    System.out.println("Inside the controller ");
    return "index";
   }

}

的web.xml

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

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

1 个答案:

答案 0 :(得分:0)

如果您有/webapp/resources/ImgPitSide.png

所以,您可以这样访问:

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

<img src="<c:url value='/resources/ImgPitSide.png'/>"/>