是这样的:
比如我现在在webroot中有个index.jsp 在index.jsp中写个表单。
现在在webroot中有个sub文件夹,sub文件夹中有个submit.jsp想得到index.jsp表单的数据index.jsp
<form action="????这边要怎么写才才能指向submit.jsp" method="post">数据</form>
比如我现在在webroot中有个index.jsp 在index.jsp中写个表单。
现在在webroot中有个sub文件夹,sub文件夹中有个submit.jsp想得到index.jsp表单的数据index.jsp
<form action="????这边要怎么写才才能指向submit.jsp" method="post">数据</form>
<action-mappings>
<action name="userForm" path="/login" scope="request"
type="com.yourcompany.struts.action.LoginAction">
<forward name="result" path="/sub/submit.jsp" />
</action>
</action-mappings>在LoginAction里面
return mapping.findForward("result");
<form action="/login" method="post">
</form>
<form action=".\sub\submit.jsp" method="post">
</form>可以了,
--------------------------
index.jsp
<form action="/sub/submit.jsp" method="post">数据</form>
<%!String basePath=request.getContextPath();%>
<form action="<%=basePath%>\sub\submit.jsp" method="post">
</form>basePath 是你项目的根路径,推荐这样做,不管以后你的sub 文件夹怎么移动,都是可以成功提交的。
/ 是相对于根目录的绝对路径
../ 是退到它的上一级目录 这里写action="./sub/submit.jsp"
就好了
先看一下下面的URL:
http://192.168.1.1:8080/hello/index.html
一个URL由
服务器地址: http://192.168.1.1:8080
WEB应用上下文: /hello
网页或请求: /index.html
三个部分组成。
WEB应用发布时,WEB应用上下文可以自行指定,如放在ROOT/下的话,WEB应用上下文就是空,如:
http://192.168.1.1:8080/index.html
它的WEB应用上下文就是空。HTML协议中要求,form的action属性,以“/”开头是绝对路径,不以“/”开头的是相对路径。
绝对路径是相对于服务器地址而言的,相对路径是相对于当前网页或请求而言的。
举一个例子,你的当前网页位置是:
http://192.168.1.1:8080/hello/page/index.html
其中/hello是WEB应用上下文;/page/index.html是网页。
在这个网页上有一个请求需要指向
http://192.168.1.1:8080/hello/servlet/checkUser.cgi
index.html中的form应该怎么写呢?
可以写成绝对路径(相对于http:/192.168.1.1:8080)
<form action="/hello/servlet/checkUser.cgi" >
...
</form>
也可以写成相对路径(相对于http://192.168.1.1:8080/hello/page)
<form action="../servlet/checkUser.cgi" >
...
</form>当前如果你的WEB应用是基于JSP的话,有许多第三方的标签库如JSTL或基于Struts的标签库都可以在form中自动为你加上WEB应用上下文,如二楼东走西顾的例子。
如果你准备在JSP使用基本语句生成网页的话,六楼honglei915提供了一个很好的例子,只不过“/”更规范一些,如:
<%!String basePath=request.getContextPath();%>
<form action="<%=basePath%>/sub/submit.jsp" method="post">
</form>
问题如下:
在<jsp:forword page="<%=basePath%>/sub/submit.jsp"/>
如果这样写,在发布出来访问这个页面居然会报错。。有人能说说原因吗?
<jsp:forword page="${basePath}/sub/submit.jsp"/>
如果这样写就可以了
</form>
居然不行
而<%!String basePath=request.getContextPath();%>
<form action=" <%=basePath%>/sub/submit.jsp" method="post">
</form>
这样方可以-----------前提都
isELIgnored="false"
了,为什么??
<action-mappings>
<action name="userForm" path="/login" scope="request"
type="com.yourcompany.struts.action.LoginAction">
<forward name="result" path="/sub/submit.jsp" />
</action>
</action-mappings>
下面的代码可以看到结果是因为contextPath被保存在page中:
<% String contextPath = request.getContextPath(); %>
<form action="<%=contextPath%>/sub/submit.jsp" method="post">
</form> EL表达式如“${contextPath}”只能从request,session及application中取值。
上述代码修改一下就可以支持EL表达式:
<%
String contextPath = request.getContextPath();
// 将contextPath保存到request中
request.setAttribute("contextPath", contextPath);
%>
<form action="${contextPath}/sub/submit.jsp" method="post">
</form>
<form action="${pageContext.request.contextPath}sub/submit.jsp" method="post">
</form> 这个应该蛮好用的吧,我一直用这个
<form action="${pageContext.request.contextPath}/sub/submit.jsp" method="post">
</form> 这个应该蛮好用的吧,我一直用这个上面粗心落了一个符号,这个可以了
<form action="${pageContext.request.contextPath}/sub/submit.jsp" method="post">
</form> 这个应该蛮好用的吧,我一直用这个上面粗心落了一个符号,这个可以了