Struts2学习 路径访问

来源:互联网 发布:淘宝上面的挪威三文鱼 编辑:程序博客网 时间:2024/05/18 00:50

一般访问某个jsp程序如下,假设index.jsp在WebContent里面

<a href="index.jsp">

若不使用structs框架,则可以访问到,但是使用了该框架,就不能这么访问了


首先structs Action 访问举个例子,在浏览器输入http://localhost:8080/project-name/packge-namespace/action-name,若是我在该链接里的某个jsp页面里 访问另一个jsp

<a href="index.jsp">

则访问路径为:http://localhost:8080/project/name/packge-namespace/index.jsp, 两个jsp访问的package-namespace是一样的,但是是访问不到index.jsp的,这说明

structs框架的路径访问 不是根据资源的位置路径来访问,而是根据Action的路径来访问,因此

Structs 路径访问资源,一定要采用绝对路径访问,最后的访问路径应该类似这样:http://localhost:8080/project-name/index.jsp


贴出一段路径访问demo

<%@ 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><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Index.jsp</title></head><base href="<%=basePath%>"><body>index index index index index index aaaaa<a href="Hello.jsp">路径问题说明</a></body></html>
期中,调用

<base href="<%=basePath%>">
可使得整个页面的所有路径自动加上basePath(basePath为http://localhost:8080/project-name),最后的访问路径类似这样:http://localhost:8080/project-name/index.jsp

0 0
原创粉丝点击