angularJs和struts2中的CRUD应用程序

时间:2016-09-02 06:21:53

标签: angularjs struts2

我想在struts2和angularJs中使用一个简单的CRUD应用程序。

我试过以下代码

addTourController.js

 var myapp = angular.module('myapp', []);
 myapp.controller('addTourCart', function($scope, $http){
 $scope.getDataFromServer = function(){
     var jouMObj={};
     var params = {
        "FromState" : $scope.TourFromState,
        "FromCity" : $scope.TourFromCity,
        "FromDate" : $scope.TourFromDate,
        "ToState" : $scope.TourToState,
        "ToCity" : $scope.TourToCity,
        "ToDate" : $scope.TourToDate,
        "Purpose" : $scope.purpose,
        "StayDetails" : $scope.TourStayMode
     };
     var data = angular.toJson(params);
     $http( {
            method : 'POST',
            url : 'addTourCart.action',
            data : 'value=' + data,
            headers : {
                'Content-Type' : 'application/x-www-form-urlencoded'
            }
        }).success(function(data){
            $scope.jouMObj = data.jouMObj;
            alert(data.jouMObj);
            alert(params.FromDate);
     });
  }
 });

jsp page

   <!DOCTYPE html>
   <html>
   <head>
   <meta charset="ISO-8859-1">
   <title>Tour</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">  </script>
</head>
<body>
<div ng-app="myapp">
          <div id="divjourneydetail" ng-controller="addTourCart">
                    <table id="table2" align="center"  cellpadding="0" cellspacing = "0">
                    <tr>
                        <th><b>From</b></th>
                        <th><b>To</b></th>
                        <th><b>Travel Details</b></th>
                        <th>Add / Reset</th>
                    </tr>
                    <tr>
                        <td><strong>State/City</strong></td>
                        <td><strong>Date</strong></td>
                        <td><strong>State/City</strong></td>
                        <td><strong>Date</strong></td>
                        <td><strong>Purpose</strong></td>
                        <td><strong>Stay Detail</strong></td>
                        <td>
                            <input type="button" id="AddTourDtl" value="Add" ng-click="getDataFromServer()"/>
                      </td>
                    </tr>
                    <tr>
                        <td>
                            <s:select name="TourFromState" id="TourFromState" list="statelist" listKey="stateCode" listValue="stateName" ng-model="TourFromState" headerKey="" headerValue="-Select State-" value="27" onchange="jFun_stateByCityList(this.value,'TourFromCity')"/>
                            <div id="tourReplaceCity">
                                <s:select name="TourFromCity" id="TourFromCity" list="citylist" listKey="regCd" listValue="distName" ng-model="TourFromCity"  headerKey="" headerValue="-Select City-" onchange="jFun_setNewCity(this.value,'getTourFromNewCity','NewTourFromCity')"/>
                            </div>
                            <div id="getTourFromNewCity"></div>
                        </td>
                        <td>
                            <input type="text" name="FromDate" id="TourFromDate" value=""  maxlength="10" ng-model="TourFromDate" >
                            <img src="<%=request.getContextPath()%>/MahBill/Images/calender_anim.gif" onClick="popupCalender('TourFromDate');"  width="19" height="19"  >
                        </td>
                        <td>
                            <s:select name="TourToState" id="TourToState" list="statelist" listKey="stateCode" listValue="stateName" ng-model="TourToState" headerKey="" headerValue="-Select State-"value="27" onchange="jFun_stateByCityList(this.value,'TourToCity')"/>
                            <div id="tourReplaceToCity">
                                <s:select name="TourToCity" id="TourToCity" list="citylist" listKey="regCd" listValue="distName" ng-model="TourToCity" headerKey="" headerValue="-Select City-" onchange="jFun_setNewCity(this.value,'getTourNewCity','NewTourToCity')"/>
                            </div>
                            <div id="getTourNewCity" ></div>
                        </td>
                        <td>
                            <input type="text" name="TourToDate" id="TourToDate" value="" maxlength="10"  ng-model="TourToDate" />
                            <img src="<%=request.getContextPath()%>/MahBill/Images/calender_anim.gif" onClick="popupCalender('TourToDate');"  width="19" height="19"  >
                        </td>
                        <td >
                            <s:textarea key="purpose" name="purpose" id="Tourpurpose" ng-model="purpose" cols="24" rows="2"/> 
                        </td>
                        <td>
                             <s:select name="TourStayMode" id="TourStayMode" headerValue="- Select -" headerKey="" ng-model="TourStayMode"
                            list="staymodelist" listKey="staymodeid" listValue="staymodename" onchange="return jFun_stayMode(this.value);"/>
                        </td>
                   </tr>
                </table>
                <div id="cartTourDetails">
                    <table id="table2" align="center"  cellpadding="0" cellspacing = "0">
                        <tr>
                            <th align="center" colspan="2" style="border-right:1px solid #333;"><b>From</b></th>
                            <th align="center" colspan="2" style="border-right:1px solid #333;"><b>To</b></th>
                            <th align="center" colspan="2" style="border-right:1px solid #333;"><b>Travel Details</b></th>
                        </tr>
                        <tr>
                            <td><strong>State/City</strong></td>
                            <td><strong>Date</strong></td>
                            <td><strong>State/City</strong></td>
                            <td><strong>Date</strong></td>
                            <td><strong>Purpose</strong></td>
                            <td><strong>Stay Detail</strong></td>
                        </tr>
                        <tr ng-repeat="journey in params">
                            <td  valign="middle">
                                {{FromState}}
                                <div id="tourReplaceCity">
                                    {{FromCity}}
                                </div>
                            </td>
                            <td>{{FromDate}}</td>
                            <td valign="middle">
                                {{ToState}}
                                <div id="tourReplaceToCity">
                                    {{ToCity}}
                                </div>
                            </td>
                            <td>{{ToDate}}</td>
                            <td>{{purpose}} </td>
                            <td>{{ToDate}}</td>
                       </tr>
                    </table>
                </div>
                </div>
 </div>

struts.xml中

 <package name="base" extends="json-default">
        <action name="addTourCart" class="Action.Tour"  method="addTourCart">
            <result type="json">
                <param name="root">jouMObj</param>
            </result>
        </action>
 </package>

模型类

   public class JourneyDetail_M implements Serializable{
   private String FromState;
   private String FromCity;
   private String FromDate;
   private String ToState;
   private String ToCity;
   private String ToDate;
   private String staymodeid;
   private String staymodename;
   private String purpose;
    //getter and setter
}

动作类

public class Tour extends ActionSupport {
    JourneyDetail_M jouMObj=new JourneyDetail_M();

public String addTourCart(){
    try {
        System.out.println(request.getParameter("value"));

        String TourFromState=request.getParameter("FromState");
        String TourFromCity=request.getParameter("FromCity");
        String TourToState=request.getParameter("ToState");
        String TourToCity=request.getParameter("ToCity");
        String TourFromDate=request.getParameter("FromDate");
        String TourToDate=request.getParameter("ToDate");
        String purpose=request.getParameter("Purpose");
        String stayMode=request.getParameter("StayDetails");

        jouMObj.setFromState(TourFromState);
        jouMObj.setFromCity(TourFromCity);
        jouMObj.setFromDate(TourFromDate);
        jouMObj.setToState(TourToState);
        jouMObj.setToCity(TourToCity);
        jouMObj.setToDate(TourToDate);
        jouMObj.setPurpose(purpose);
        jouMObj.setStaymodeid(stayMode);
    } catch (IOException e) {
        e.printStackTrace();
        return "error";
    }
    return SUCCESS;
}
public JourneyDetail_M getJouMObj() {
    return jouMObj;
}
public void setJouMObj(JourneyDetail_M jouMObj) {
    this.jouMObj = jouMObj;
}

 }

这是我的代码 在使用request.getParameter(&#34; value&#34;)的行动中,我得到了所有的价值,但是如何从行动类中设置和获取?

2 个答案:

答案 0 :(得分:1)

我不熟悉AngularJS ....但data : 'value=' + data,行意味着您将所有数据作为单个请求参数传递给您的操作(value)。 struts无法读取value并为您填充JourneyDetail_M对象。

您需要使用一些gson实用程序类将request.getParameter("value")反序列化为您的对象。

或者您可以使用Angularjs - simple form submit中提到的不使用angular.toJson的html提交。这样,所有参数都单独发送。使你的struts动作ModelDriven,你的模型可以用struts填充。

可能是它的命名问题......你的动作属性名称可能是camelCase。因此,例如,在您的操作中将FromState重命名为fromState,并确保{j}将{j}中的输入名称设置为public setFromState (String ... )

使用struts操作时,让struts填充你的动作属性,尽量不要使用请求。

答案 1 :(得分:1)