当前位置:编程学习 > JAVA >>

strut2的一个ajax疑问

小弟初学struts2,新建一个叫ZS的工程,在用jquery进行ajax操作时碰见一个问题,struts配置时候,如果package的namespace为空时,url不需要加上工程名就能访问action,但是namespace不为空时url必须加上工程名(ZS)才能访问到action,这是为什么,怎么才能在namespace不为空时不加工程名也能访问到action(即url="test/selectDemo.action";)。

代码贴下面了:

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"></include>

  <package name="demohello" namespace="/test" extends="struts-default">
    <action name="selectDemo" class="com.ehl.demo.DemoAction" method="selectDemo">
   </action>
 
 </package>

</struts>


demo.js


function test(){
var url="/ZS/test/selectDemo.action";  
 $.ajax({
      url:url,    
       type: 'POST', 
       dataType: 'xml',
       data: {name:"admin"},                
       timeout: 1000,                           
       error: function(){                      
       alert('Error loading XML document'); 
       }, 
       success: function(xml){            
        alert(xml.xml);
        
      } 
  });
}





 public String selectDemo() throws  IOException {
//  HttpServletRequest request = ServletActionContext.getRequest(); 
HttpServletResponse response = ServletActionContext.getResponse(); 
    response.setContentType("text/xml;charset=utf-8"); 
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-Control", "no-cache"); 
    PrintWriter out=response.getWriter();        
    
    System.out.println("000000001");
   StringBuilder sb=new StringBuilder();
   sb.append("<?xml version='1.0' encoding='UTF-8'?><provinces>");
   for(int i=0;i<10;i++)
  {
    sb.append("<province>");
    sb.append("<provinceid>name_"+i+"</provinceid>");
    sb.append("<provinceName>"+i+"</provinceName>");
    sb.append("</province>");
  }
  sb.append("</provinces>");
  out.print(sb.toString());
  out.flush();
 out.close();
return null;
}
--------------------编程问答-------------------- 貌似必须加工程名吧!不然请求路径怎么找得到呢! --------------------编程问答-------------------- 那为什么namespance为空时可以 --------------------编程问答-------------------- 在test前面加上/,因为你的namespace="/test" --------------------编程问答-------------------- 在package配置中namespace,methods都有默认值,action中的name也是有的 --------------------编程问答-------------------- 因为访问路径最好用绝对路径。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

配置好这个节点
<head>
    <base href="<%=basePath%>"></base> --------------------编程问答--------------------
引用 4 楼 xuerenlv 的回复:
在package配置中namespace,methods都有默认值,action中的name也是有的

没明白什么意思,不用绝对路径,用相对路径不行吗 --------------------编程问答-------------------- url="test/selectDemo.action";
这样做肯定不行啊
你把test必为项目的URL就好了
如:http://127.0.0.1:8080/selectDemo.action --------------------编程问答-------------------- <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
强力要求LZ加上项目的工程路径,如果下次调用的是其他namesapce里面的action,的配置项目的全路径才访问! --------------------编程问答-------------------- ajax里面的URL,你的ZS也不能写死了,如果部署到生产上,和你的项目名称不一致也就错误,所以加上项目的<%=path> --------------------编程问答-------------------- 试一下/test/selectDemo.action或者用../test/selectDemo.action试一下;这两个路径应该有一个是对的。 --------------------编程问答-------------------- 你这样,将项目的web Context-root 改成 /
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,