开发者

Struts2 json ajax and also custom return type

I am trying to do a result type of "json" from my struts2 action. Here is the configuration I have in my code.

Struts.xml:

<package name="example" namespace="/" extends="json-default">
    <action name="vendorList" class="com.stg.providerportal.actions.AjaxVendorListAction">
      <result  name="success" type="json"></result>
    </action>
</package>

Struts Action:

private Object jsonModel;


 public String execute() throws Exception {

  String nextStep = SUCCESS;

  Map session = getSession();
  SessionParams params = (SessionParams) session.get(Constants.KEY_SESSION_PARAMS);

  try {
   Map json = new HashMap<String,String>();
   json.put("apple", "Hi");
   //vendorList = populateVendors(params,taxID,billingZipCode);
   setJsonModel(json);
   //session.put(Constants.KEY_SESSION_VENDOR_LIST, vendorList);

  } catch (Exception e) {
   CILogUtil.logInfo(e, params);
   nextStep = AppConstants.SYSTEM_UNAVAILABLE;
  }

  return SUCCESS;
 }

I am calling this action using ajax(jquery) and here how i make a call.

$.getJSON('vendorList.action' ,  
 {
  "taxID": taxID,
  "billingZipCode": billingZipCode      
 },
 function(json) {
 alert('hi');
 });

$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
    alert(textStatus);
    alert(errorThrown);
    alert(XMLHtt开发者_JAVA技巧pRequest.responseText);
}});

When i execute this I am getting back the response from the action as below. Dont know why.

Error 404: SRVE0190E: File not found: /vendorList.action 

Even when I am trying to run this action directly from the browser, I get the same response.

http://localhost:9081/providerportal1/vendorList.action?taxID=111billingZipCode=1111


Do you have the struts2 filter(s) configured appropriately in your web.xml file? At a minimum, you should have your web.xml configured like this example -

http://s.apache.org/ata

Also, did you see any errors in your tomcat logs when the web-app started? Typically, if the struts2 filter can't find an action definition to match the request, it will create an error like the following -

HTTP Status 404 - There is no Action mapped for namespace / and action name vendorList.action.

The 404 you pasted looks more like it came from Tomcat which would indicate that struts2 is probably not properly incorporated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜