Java 图像未显示在带有spring的jsp中
我不熟悉使用jsp的spring,我基本上是想在jsp中显示一个图像Java 图像未显示在带有spring的jsp中,java,jsp,url,maven,spring-mvc,Java,Jsp,Url,Maven,Spring Mvc,我不熟悉使用jsp的spring,我基本上是想在jsp中显示一个图像 JSP页面 Spring 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:p="http://www
JSP页面
Spring 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<mvc:resources location="/images/" mapping="/images/**"/>
<!-- Application controllers package -->
<context:component-scan base-package="net.ignou.onlinetest.controller" />
<bean id="viewoseesolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/online_test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>net.ignou.onlinetest.domain.Question</value>
<value>net.ignou.onlinetest.domain.Student</value>
<value>net.ignou.onlinetest.domain.Answer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<bean id="questionDao" class="net.ignou.onlinetest.dao.daoImpl.QuestionDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="loginDao" class="net.ignou.onlinetest.dao.daoImpl.LoginDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="answerDao" class="net.ignou.onlinetest.dao.daoImpl.AnswerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="service" class="net.ignou.onlinetest.service.serviceImpl.ServiceImpl">
<property name="questionDao" ref="questionDao"/>
</bean>
</beans>
/WEB-INF/views/
.jsp
net.ignou.onlinetest.domain.Question
net.ignou.onlinetest.domain.Student
net.ignou.onlinetest.domain.Answer
org.hibernate.dialogue.mysql5innodbdialogue
真的
没有一个
My web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Person Detail</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
人员详细信息
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/
index.jsp
我正在使用maven。我的图像文件位于
在线测试\src\main\webapp\images
我的jsp页面在
在线测试\src\main\webapp\web inf\views
我还尝试将src替换为
,但没有成功。我还尝试将我的jsp和图像直接移动到webapp文件夹,但没有用。我做错了什么吗spring如何处理img请求?当涉及URL路径时,JSP文件的位置是不相关的。重要的是浏览器显示的页面位置,即浏览器地址栏中显示的页面地址
因此,如果页面的地址是/webapp/foo/bar/someAction.html
,图像位于/webapp/images/top.jpg
,那么路径应该是/webapp/images/top.jpg
(绝对路径,首选且更清晰),或者。/../images/top.jpg
(相对路径,如果移动文件或更改URL,则更难重构)
我的建议是:始终使用绝对路径,并使用JSTL的c:url标记避免硬编码webapp的上下文路径:
<img src="<c:url value='/images/top.jpg'/>"/>
“/>
上面这一行将始终有效。您要做的是在SpringServlet上下文xml配置中添加这一行
<mvc:resources mapping="/images/**" location="/images/" />
标记是在Spring3.0.4中引入的,因此您至少需要该版本的spring和xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd`
此外,正如JB Nizet所说,您应该将您的图像引用为
<img src="<c:url value='/images/top.jpg'/>"/>
“/>
相对路径。处理图像请求的不是spring,而是您的浏览器。您有spring的资源处理程序映射吗?没有,我只有视图解析器。步骤1:./images/等等步骤2:打开firebug并尝试输入路径,看看它是如何工作的works@Lakshmiweb应用程序的上下文根是什么?在你的问题中加入完整的servlet上下文配置,这样我们就可以看到是否一切正常了。在我的问题中添加的代码还有一件事我得到了SaxParserException关于添加mvc:resources的建议虽然我添加了模式定义,但这是匹配通配符严格的原因,但是找不到元素“mvc:resources”@Lakshmi Use
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
。resources
标记是在Spring3.0.4中引入的。如中所述,不要在mvc
的模式位置中使用后缀-3.0
。您使用的是哪个版本的spring?您不需要多个
标记,只需要一个带有通用映射的标记,例如/resources/**,以及一个逗号分隔的位置列表,例如/resources/css、/resources/js。请参阅答案中的更新。不起作用。请单独尝试,并与Sotirios Delimanolis解决方案一起尝试。
<img src="<c:url value='/images/top.jpg'/>"/>