刚练struts,想简单地通过链接格式<a href>来访问web-inf自定义文件夹里的jsp。我的思路是写个action来间接访问。但调了一天了,真不知自己哪里错了,总是访问不了。麻烦大家帮忙看看。相关代码如下:
struts-config.xml
......
<action
  attribute="exerciseListForm"
  name="exerciseListForm"
  path="/exerciseList"
  scope="request"
  type="com.ustc.struts.action.ExerciseListAction" >
  <forward name="ch2List" path="/WEB-INF/jsp/ch2/exerciseList.jsp"></forward>
</action>welcome.jsp
 ......
  <a href="exerciseList.do?actionId='ch2List'">ch2</a>ExerciseListAction.java
......
public class ExerciseListAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { ExerciseListForm exerciseListForm = (ExerciseListForm) form;// TODO Auto-generated method stub
return mapping.findForward(exerciseListForm.getActionId());
}
}另外,ExerciseListForm.java里的变量就是传递的变量actionId但总是报错如下:
HTTP Status 404 - /Project02/WEB-INF/jsp/exerciseList.do
--------------------------------------------------------------------------------
type Status report
message /Project02/WEB-INF/jsp/exerciseList.do
description The requested resource (/Project02/WEB-INF/jsp/exerciseList.do) is not available.我认真分析了下,觉得原因是<a href>里写的action根本就没有提交给ActionServlet,而是直接简单的将<a href>所在的页面的路径(/Project02/WEB-INF/jsp/)加上我在超链接里写的内容(exerciseList.do?actionId='ch2List')后,就直接在浏览器里访问了(浏览器里显示的地址也是:http://localhost:8080/Project02/WEB-INF/jsp/exerciseList.do?actionId=ch2List)诚心请教大侠该怎么做才能成功的实现welcome.jsp页面里用超链接跳转到/WEB-INF/jsp/ch2/exerciseList.jsp

解决方案 »

  1.   

    <a href="exerciseList.do?actionId='ch2List'">ch2</a>
    这个好像不对的。
    你可以先把它换成一个绝对URL。
    如:http://localhost:8080/yourApp/exerciseList.do?actionId=ch2List
    这样来试试。
    如果你直接这样写:
    href="exerciseList.do?actionId='ch2List'
    这个路径,应该是将你当前jsp所在路径加在前面的url。
    比如你当前jsp是:/Project02/WEB-INF/jsp/test.jsp
    那么超级链接的完整url就是:
    http://localhost:8080/Project02/WEB-INF/jsp/exerciseList.do?actionId='ch2List'
      

  2.   

    谢谢楼上的回复,我试了,还是不行。忘了说,
    我的当前页面welcome.jsp的路径是/Project02/WEB-INF/jsp/welcome.jsp;
          超链接想访问的jsp的路径是/Project02/WEB-INF/jsp/ch2/exerciseList.jsp。
      

  3.   

    两种方法,你都试一下:
    <a href="/exerciseList.do?actionId=ch2List">ch2</a>
    <a href="http://localhost:8080/Project02/exerciseList.do?actionId=ch2List">ch2</a>这两个应该是等效的。
    第二个如果还不行的话,你就应该检查你的web.xml和strut的配置了。
      

  4.   

    'ch2List'
    你这个东东不是个字符串吗?如果是个变量的话应该用EL表达式括起来才对呀...
      

  5.   

    谢谢bayougeng,不过还是不行。我的web.xml相关配置如下。struts-config.xml的配置在前面。麻烦帮我再看看怎么回事。另外,你写的<a href="/exerciseList.do?actionId=ch2List">ch2</a>有点问题。路径前有“/”就表示是绝对路径了。所以不该加“/”。
    web.xml
       ....
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        ....
      </servlet>  <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      

  6.   

    你确认:
    <a href="http://localhost:8080/Project02/exerciseList.do?actionId=ch2List">ch2</a>
    这个不行么?
    把浏览器的缓存清理以后再试。
      

  7.   

    <a href="/exerciseList.do?actionId=ch2List">ch2</a>
    这样写我不确定是不是正确了。
    但我认为没有问题的。
    对于一个web app来说,它不会跳转到另外一个web app的,浏览器也不可能知道你想跳转到哪个web app。
    所以说。/表示当前web app的根,也就是:http://localhost:8080/yourApp。
    而/exerciseList.do就应该表示:http://localhost:8080/yourApp/exerciseList.do了。
      

  8.   

    但总是报错如下:
    HTTP Status 404 - /Project02/WEB-INF/jsp/exerciseList.do
    --------------------------------------------------------------------------------
    你的action访问路径是:path="/exerciseList"
    我的一个计数器问题(关于相对路径和绝对路径的问题)  http://www.phome.asia/forum/thread/6072.html