java之struts2之ajax

2020-12-13 06:00

阅读:529

标签:htm   out   file   jackson   ssis   commons   类型   func   system   

1.Ajax 技术在现有开发中使用非常多,大多是做管理类型系统。在servlet中可以使用ajax。在struts2中共还可以使用servlet的方式来实现ajax。

2.案例:用户名检查

public String checkName() throws IOException{
        HttpServletResponse resp = ServletActionContext.getResponse();
        if("siggy".equals(name)){
            resp.getWriter().print("true");
        }else{
            resp.getWriter().print("false");
        }
        //return Action.NONE;
        return null;
    }

3.使用struts2提供的插件来完成 ajax步骤

  a) 导入相关 jar 包

asm-3.3.jar

asm-commons-3.3.jar

asm-tree-3.3.jar

commons-beanutils-1.8.0.jar

commons-collections-3.1.jar

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

commons-lang-2.4.jar

commons-lang3-3.1.jar

commons-logging-1.1.1.jar

ezmorph-1.0.6.jar

freemarker-2.3.19.jar

jackson-core-asl-1.9.2.jar

jackson-mapper-asl-1.9.2.jar

javassist-3.11.0.GA.jar

json-lib-2.3-jdk15.jar

jstl-1.2.jar

log4j-1.2.17.jar

ognl-3.0.5.jar

struts2-core-2.3.4.jar

struts2-json-plugin-2.3.4.jar

xwork-core-2.3.4.jar

  b) 编写Action

public class UserAction {
    private String name;
    private String result;
    public String checkName() throws IOException{
        HttpServletResponse resp = ServletActionContext.getResponse();
        if("siggy".equals(name)){
            resp.getWriter().print("true");
        }else{
            resp.getWriter().print("false");
        }
        //return Action.NONE;
        return null;
    }
    public String list(){
        System.out.println("list----------");
        List list = new ArrayList();
        list.add(new User("张三","男",33));
        list.add(new User("李四","男",23));
        list.add(new User("王五","男",13));
        try {
            result=JSONArray.fromObject(list).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return Action.SUCCESS;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }

}

  c) 编写配置文件

package name="default" extends="json-default" namespace="/">
        class="cn.sxt.action.UserAction" method="checkName">
        
        class="cn.sxt.action.UserAction" method="list">
            result
            
        
    package>

4.另一种获取 json 的方式:

public class ListAction {
    private List list;

    public String list(){
        list = new ArrayList();
        list.add(new User("张三","男",33));
        list.add(new User("李四","男",23));
        list.add(new User("王五","男",13));
        return Action.SUCCESS;
    }
    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
    
}

Struts.xml

class="cn.sxt.action.ListAction" method="list">
            list
            

Jsp


    
姓名 性别 年龄

 

java之struts2之ajax

标签:htm   out   file   jackson   ssis   commons   类型   func   system   

原文地址:https://www.cnblogs.com/Vincent-yuan/p/11161295.html


评论


亲,登录后才可以留言!