遇到这样的问题,在地址栏上这样访问。
http://localhost:8080/user/user!login.action
struts文件这么配置
<package name="user" namespace="/user" extends="struts-default">
<action name="user!*" class="com.q167.action.LoginAction" method="{1}">
<result name="success">/ok.jsp</result>
<result name="fail">/error.jsp</result>
</action>
</package>
ok.jsp 页面中有这样的代码
<img src="./img/ok.jpg">
这个时候这个ok.jpg的图片不能正常显示,错误信息是:
GET http://localhost:8080/user/img/ok.jpg 404 (Not Found)
而平常你用 http://localhost:8080/ok.jpg 这个图片是正常显示的。
其实意思就是因为你那个action是在namespace='/user' 下触发的,所以那个html里的
图片也被加了一层目录。(虽然这个action的结果跳转到位于webcontent下的ok.jsp,但是它当前的rootPath貌似变化了,就是加了一层/user)
文件路径如下:
\WebContent
---\img
---ok.jpg
---ok.jsp
我们这里的一个技巧就是要让html里的相对位置的引用别跟着那个被触发的action
的namespace去变动。具体做法如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1><font color="green">It Works!!!</font></h1>
<img src="./img/ok.jpg">
</body>
</html>
重点在于
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>">
分享到:
相关推荐
<td><a href='addUser.jsp'>添加</a> <a href='<%=basePath%>DelUserServlet?id=<%=users.getId()%>'>删除</a> <a href='updateUser.jsp'>更新</a> <a href="login.jsp">登陆</...
<base href="<%=basePath%>"> <title>用户注册</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> ...
spring 的实例源码 <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> ...
<base href="<%=basePath%>"> <title>My JSP 'text1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<html><br> <head><br> <base href="<%=basePath%>"><br> <br> <meta http-equiv="content-type" content="text/html;charset=UTF-8"/> <br> <br> <br> <meta http-equiv="pragma" content="no-cache"><br> <meta ...
<base href="<%=basePath%>"> <title>My JSP 'text.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>Login Page</title> <!-- Meta tags for SEO --> ... </head> <body> <% if (username != null) { %> 用户名: <%= username %> <br/> <% } %> <!-- Display the form ...
<link href="<%=basePath %>css/base.css" rel="stylesheet" type="text/css"> <link href="<%=basePath %>css/reg.css" rel="stylesheet" type="text/css"> <link rel="stylesheet" href="<%=basePath %>css/...
<base href="<%=basePath%>"> <title>动漫搜索</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> ...
<base href="<%=basePath%>"> <title>提交网页</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> ...
<base href="<%=basePath%>"> <title>My JSP 'userRegister.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>入口页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> ...
<base href="<%=basePath%>"> <title>主页面</title> </head> <body> <div align="center"> <table> <tr> <th>主页面</th> </tr> </table> </div> <jsp:include page="4-3.jsp"> <jsp:param value="John...
<base href="<%=basePath%>"> <title>MyJSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>MyJSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>中国证券会证券期货违法违规举报中心-注册</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>MyJSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...
<base href="<%=basePath%>"> <title>MyJSP 'insdata.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta ...