Struts2的自动封装注入

 我们在使用struts2进行从前台传到后台的时候(当然这里是在结合了form表单的情况下),我们通常会给input框中的name属性赋值,然后再 后台action中提供setter/getter方法就可以完成数据的传递(在jquery中使用ajax的时候要记 得$("form").serialize()给表单的数据序列化)。这种方式用起来虽然很顺手,但是还有一种更加方便的方法,就是使用struts2的 自动封装,且看具体做法:
1.在后台创建一个实体类(实体类中的属性要和前台的对应,请看下面前台.jsp的代码):
public class User {
   
    private String id;
    private String uerName;
    private String password;
    private String idCard;
    private String address;
    private String sex;
    private String email;
    private String phone;
   //下面为各个属性提供setter/getter方法,这里不再展示
   
}
2.创建一个action类(提供User实体类的setter/getter方法):
public class UserAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
   
   
    private User user;
   
    HttpServletRequest request;
    HttpServletResponse response;
   
    public UserAction(){
        response = ServletActionContext.getResponse();
        request = ServletActionContext.getRequest();
    }
    public void addUser(){
        System.out.println("####"+user.getUerName());//在这里可以看的到输出结果
        try {
            response.getWriter().print("ok");
        } catch (IOException e) {
            e.printStackTrace();
        };
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}
3.创建一个addUser.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'addUser.jsp' starting page</title>
</head>

<body>
    <div id="addBox">
        <form id="addUser" method="POST">
            <div id="uID">
<!-- 注意在input框里面的name值为对象点属性,这里的点属性一定要和User实体类里面的的属性相同 -->
                <label class="tip">用户编号:</label><span class="field"><input name="user.id"
                    type="text"></span>
            </div>
            <div id="uName">
                <label class="tip">用户名:</label><span class="field"><input name="user.uerName"
                    type="text"></span>
            </div>
            <div id="uPassword">
                <label class="tip">密码:</label><span class="field"><input name="user.password"
                    type="password"></span>
            </div>
            <div id="uidCard">
                <label class="tip">身份证号:</label><span class="field"><input name="user.idCard"
                    type="text"></span>
            </div>
            <div id="uEmail">
                <label class="tip">邮箱:</label><span class="field"><input name="user.email"
                    type="text"></span>
            </div>
            <div id="uPhone">
                <label class="tip">联系方式:</label><span class="field"><input name="user.phone"
                    type="text"></span>
            </div>
            <div id="uAddress">
                <label class="tip">住址:</label><span class="field"><input name="user.address"
                    type="text"></span>
            </div>
            <div id="uSex">
                <label class="tip">性别:</label><span class="field"> <label><input
                        name="user.sex" type="radio" value="男">男</label> <label><input
                        name="user.sex" type="radio" value="女">女</label>
                </span>
            </div>
            <div id="btn">
                <button id="addBtn">添加</button>
                <!-- <input type="button" onclick="loadInfo();" value="添加"/> -->
            </div>
        </form>
    </div>
</body>
<script type="text/javascript" src="script/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    $("#addUser").submit(function(){
        var ajax_url = "users_addUser.action"; //表单目标
        var ajax_type = $(this).attr('method'); //提交方法
        var ajax_data = $(this).serialize();
         $.ajax({
            url:ajax_url,
            type:ajax_type,
            data:ajax_data,
            success:function(data){
                if(data=="ok"){
                    alert("添加成功");
                }else{
                    alert("添加失败");
                }
            },
            error : function() {
                alert("网络错误");
            }
        });
        return false; //阻止表单的默认提交事件
    });
</script>
</html>
注意事项:前台的input框里面的name值切记要和实体类里面对应,并且在action中为实体类提供setter/getter方法;当然使用struts2是要配置的,如想要了解可以私聊。

posted @ 2016-05-17 21:18  冬雷小墨  阅读(1768)  评论(0编辑  收藏  举报