struts2 对外实现接口步骤

1.在struts.xml文件中配置路径映射的Action

struts2 对外实现接口步骤_第1张图片

 

图中 1 为定义的url,2为要映射的Action,result标签可以控制执行完Action中的逻辑后跳转的页面,

例如:“http://localhost:8080/imCroePlatform/get/custOrderLogistics?commonRegionId=123&extCustOrderId=123”。

 

2.Action类继承 ActionSupport将url中的参数定义为类属性,并实现其get、set方法

例如:“http://localhost:8080/imCroePlatform/get/custOrderLogistics?commonRegionId=123&extCustOrderId=123

struts2 对外实现接口步骤_第2张图片

3.实现excute方法,在此方法中可以直接获取到url参数的值

struts2 对外实现接口步骤_第3张图片

补充:

1.如果url没有特定的谓词结尾,例如:*.do、*.action等,请检查web.xml中是否添加了相关映射

struts2 对外实现接口步骤_第4张图片

2.Action返回值可以通过流的方式返回至页面

public String outPutString(String param){
        PrintWriter writer = null;
        try {
            HttpServletResponse response = ServletActionContext.getResponse();        
            response.setContentType("application/json;charset=utf-8");
            writer = response.getWriter();
            response.setDateHeader("Expires", 0);
            writer.write(param);
            writer.flush();
        } catch (Exception e) {
            log.info("outPutString:"+e);
        }finally{
            if(writer!=null){
                writer.close();
            }
        }
        return null;
    }

你可能感兴趣的:(struts框架)