无法将对象从操作类传递到Jsp [英] Cannot Pass Object from Action Class to Jsp

查看:62
本文介绍了无法将对象从操作类传递到Jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将struts2与Conventional插件结合使用angularjs.我有一个动作类和jsp,如下所示:

I use struts2 with convention plugin combined with angularjs. I have an action class and jsp as below:

IndexAction.java

package sm.hris.struts2.base.modules.order;

import java.util.ArrayList;
import java.util.Date;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import sm.hris.struts2.base.SmBaseAction;
import sm.hris.struts2.base.db.Order;
import sm.hris.struts2.base.db.OrderDAO;
import sm.hris.struts2.base.modules.order.IndexAddAction;


@Results({
    @Result(name="add", location="/base/modules/order/index-add", type="redirect"),
    })
@ParentPackage(value = "hris")

public class IndexAction extends SmBaseAction {
    private static final long serialVersionUID = 7353477345330099548L;
    private Order order = new Order();
    private OrderDAO orderDAO = new OrderDAO();
    private ArrayList<Order> orders;
    private String idOrder = new String();
    private String searchKey = new String();
    private ArrayList<String> formArg = new ArrayList<String>();
    private ArrayList<String> idOrders = new ArrayList<String>();
    private String proc = new String();
    private String res = new String();
    private IndexAddAction indexAddAction = new IndexAddAction();

    public String execute() throws Exception{
        //super.listMenu();
        if(proc.equals("Add")){
            order.setOrderDate(new Date());
            orderDAO.setOrder(order);
            String strIdOrderCounter = orderDAO.orderAdd();
            ArrayList<String> argArray = new ArrayList<String>();
            argArray.add(0,strIdOrderCounter);
            orderDAO.setArgArray(argArray);
            orders = orderDAO.searchOrderByIdOrder();
            order = orders.get(0);
            res= "add";
        }
        if(proc.equals("Delete")){
            res = orderDelete();
        }
        if(!(proc.equals("Add")||proc.equals("Delete"))){
            if (!searchKey.equals("")) {
                formArg.add("%"+searchKey+"%");
                orderDAO.setArgArray(formArg);
                orders = orderDAO.searchOrderByAnyLike();
                res="success";
            } 
            else {
                orders = orderDAO.searchOrder();
                res="success";
            } 
        }
        return res;
    }

    public String orderDelete() throws Exception {
        orderDAO.setArgArray(idOrders);
        orderDAO.orderDelete(); 
        return "success";
    }

    public String orderAdd() throws Exception {
        return "add";
    }


    //---- Getter Setter ----// 

    public String getIdOrder(){
        return idOrder;
    }

    public void setIdOrder(String idOrder){
        this.idOrder=idOrder;
    }

    public String getSearchKey(){
        return searchKey;
    }

    public void setSearchKey(String searchKey){
        this.searchKey=searchKey;
    }

    public ArrayList<String> getIdOrders(){
            return idOrders;
        }

    public void setIdOrders(ArrayList<String> idOrders){
            this.idOrders=idOrders;
        }

    public ArrayList<Order> getOrders(){
        return orders;
    }

    public void setOrders(ArrayList<Order> orders){
        this.orders = orders;
    }

    public Order getOrder(){
        return order;
    }

    public void setOrder (Order order){
        this.order = order;
    }

    public void setProc(String proc) {
        this.proc = proc;
    }



}

index-add.jsp :


<!DOCTYPE html>
<html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sb" uri="/struts-bootstrap-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="hris, company, resources, management, showcase" />
    <meta name="description" content="A Showcase for the Human Resporce Management System" />
    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

    <![endif]-->
    <sb:head includeScripts="true" includeScriptsValidation="false"/>
    <sj:head jqueryui="true"/>
    <style type="text/css">
        body {
            padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
        }
        h5 {padding-left: 210px;}
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
    <!-- 
    <script data-require="angular.js@1.3.15" data-server="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
    -->
    <script>
    //function show_unitlist() {
    //  dojo.event.topic.publish("show_unitlist");
    //}
    </script>

    <script>

     var app = angular.module("orderApp", []);

     app.controller("orderAppCtrl", function($scope,$http,$window,$compile) {

       //$scope.orderDetails = [{id: 'orderDetail1', name: 'orderDetail1'}, {id: 'orderDetail2', name: 'orderDetail2'}, {id: 'orderDetail3', name: 'orderDetail3'}];

        $scope.orderDetails = [];
        $scope.orderDetail={
            'idOrderDetail' :"",
            'idOrder'       :"",
            'idProduct'     :"",
            'amount'        :"",
            'unit'          :"",
            'unitPrice'     :"",
            'subTotal'      :""
        };

        $scope.addNewOrderDetail = function() {
            var murl ="/sm-hris/base/modules/orderdetail/select-id-order-detail-counter-json";
            $http.get(murl)
            .then(function(response) {
                $scope.idOrderDetailCounter = response.data.idOrderDetailCounter;
            });


            $scope.orderDetails.push({
                'idOrderDetail' : ($scope.idOrderDetailCounter),
                'idOrder'       :'<s:property value="order.idOrder"/>',
                'idProduct'     :'',
                'amount'        :'',
                'unit'          :'',
                'unitPrice'     :'',
                'subTotal'      :''
            });
       };


       $scope.removeNewOrderDetail = function(nId) {
            //var index = $scope.orderDetails.indexOf(item);
            //$scope.orderDetails.splice($scope.orderDetails.indexOf(nId), 1); 
            //var oOrderDetail = $scope.orderDetails.filter(orderDetail = function() {return orderDetail.id === nId});
            //var vIndex = $scope.orderDetails.indexOf(oOrderDetail);
            //var vIndex = $scope.orderDetails.findIndex(orderDetail=>orderDetail.id === nId);
            var found = $scope.orderDetails.find(function(orderDetail){return orderDetail.id = nId});
            var vIndex = $scope.orderDetails.indexOf(found);
            $scope.orderDetails.splice(vIndex,1);
            //$scope.orderDetails.splice(nId,1);     
       };

       $scope.showAddOrderDetail = function(orderDetail) {
         return orderDetail.id === $scope.orderDetails[$scope.orderDetails.length-1].id;
       };

       $scope.idProductNgBlur = function (idProduct,idx){
           $scope.orderDetails[idx].unitPrice = 0;
           var murl ="/sm-hris/base/modules/orderdetail/select-product-by-id-json?idProduct="+ idProduct;
            $http.get(murl)
            .then(function(response) {
                var product = response.data.products[0];
                $scope.orderDetails[idx].unitPrice = product.unitPrice;
                $scope.orderDetails[idx].subTotal = $scope.orderDetails[idx].amount * $scope.orderDetails[idx].unitPrice;

            })   
       }

       $scope.amountNgBlur = function (amount,unitPrice,idx){
        $scope.orderDetails[idx].subTotal = amount * unitPrice;
       }
     });

    </script>

</head>


<body ng-app="orderApp">

<div class="container" ng-controller="orderAppCtrl">
    <div class="row">
          <div class="panel-heading">
            <h1>Adding Order</h1>
          </div>
            <div class="container">

            <!-- <s:form id="frmOrder" action="index-edit" enctype="multipart/form-data" theme="bootstrap" cssClass="form-horizontal"> -->
                <div class="form-group">
                <div class="row">
                    <div class="col-md-9">
                        <s:textfield
                            label="Order ID"
                            name="order.idOrder"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Enter ID Order"
                            value="%{order.idOrder}"
                            ng-model="idOrder"
                            readonly="true"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Total"
                            name="order.total"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Total"
                            value="%{order.total}"
                            ng-model="total"
                            readonly="true"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Total Discount"
                            name="order.totalDiscount"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Total Discount"
                            value="%{order.total}"
                            ng-model="totalDiscount"
                            readonly="true"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="VAT"
                            name="order.vat"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="VAT"
                            value="%{order.vat}"
                            ng-model="vat"
                            readonly="true"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Cash"
                            name="order.cash"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Cash"
                            value="%{order.cash}"
                            ng-model="cash"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Changes"
                            name="order.changes"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Changes"
                            value="%{order.changes}"
                            ng-model="changes"
                            readonly="true"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Payment Method Id"
                            name="order.idPaymentMethod"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Payment Method Id"
                            value="%{order.idPaymentMethod}"
                            ng-model="idPaymentMethod"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Payment Remark"
                            name="order.paymentRemark"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Payment Remark"
                            value="%{order.paymentRemark}"
                            ng-model="paymentRemark"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:textfield
                            label="Order Date"
                            name="order.orderDate"
                            cssClass="input-sm"
                            elementCssClass="col-sm-3"
                            tooltip="Order Date"
                            value="%{order.orderDate}"
                            ng-model="orderDate"
                            readonly="true"
                            />
                    </div>
                    <div class="col-md-9">
                        <s:submit cssClass="btn btn-primary" id="proc" name="proc" value="Save" />
                    <!--  <button ng-click="orderAddClick()">Add</button> -->
                    </div>
                    <div class="row">
                    <div class="col-md-9">
                      <h1>Order Detail</h1>
                      <div class="row" ng-repeat="orderDetail in orderDetails">
                        <!-- <s:submit cssClass="btn btn-primary" ng-click="removeNewOrderDetail('{{orderDetail.id}}')" value="Remove Order Detail" /> -->
                        <div class="col-md-3">
                        <s:textfield type="text" ng-if="orderDetail.idOrderDetail" ng-model="orderDetail.idOrderDetail" name="orderDetails[{{$index}}].idOrderDetail" id="orderDetails[{{$index}}].idOrderDetail" placeholder="Id Order Detail" value="{{orderDetail.idOrderDetail}}" /></div>
                        <div class="col-md-3">
                        <s:textfield type="text" ng-if="orderDetail.idOrderDetail" ng-model="orderDetail.idProduct" name="orderDetails[{{$index}}].idProduct" id="orderDetails[{{$index}}].idProduct" placeholder="Id Product" value="{{orderDetail.idProduct}}" ng-blur="idProductNgBlur(orderDetail.idProduct,$index)"/></div> 
                        <div class="col-md-2">
                        <s:textfield type="text" ng-if="orderDetail.idOrderDetail" ng-model="orderDetail.amount" name="orderDetails[{{$index}}].amount" id="orderDetails[{{$index}}].amount" placeholder="Amount" value="{{orderDetail.amount}}" ng-blur="amountNgBlur(orderDetail.amount,orderDetail.unitPrice,$index)"/></div>
                        <div class="col-md-2">
                        <s:textfield type="text" ng-if="orderDetail.idOrderDetail" ng-model="orderDetail.unitPrice" name="orderDetails[{{$index}}].unitPrice" id="orderDetails[{{$index}}].unitPrice" placeholder="Unit Price" value="{{orderDetail.unitPrice}}" /></div>
                        <div class="col-md-2">
                        <s:textfield type="text" ng-if="orderDetail.idOrderDetail" ng-model="orderDetail.subTotal" name="orderDetails[{{$index}}].subTotal" id="orderDetails[{{$index}}].subTotal" placeholder="Sub Total" value="{{orderDetail.subTotal}}" /></div>
                      </div>
                     </div>
                     </div>
            <!-- </s:form>  --> 
            <div class="row">
                <s:submit cssClass="btn btn-primary" id="addOrderDetail" ng-click="addNewOrderDetail()" value="Add Order Detail" />
            </div>
            </div>
        </div> 
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col-md-10">
            <p class="pull-right"><a href="#">Back to top</a></p>
        </div>
    </div>
</div>  
</body>
</html>

我的问题是:为什么,似乎订单没有从动作类传递给jsp.从order.idOrder可以看出我没有尝试显示在jsp中.

My question is: Why, it seems like the order doesn't get pass to the jsp from the action class. I can tell from the order.idOrder that I tried to displayed in the jsp is not displayed.

有人可以帮忙吗?非常感谢

Could someone help? Thanks a lot

推荐答案

如何在Web应用程序中结合Struts2和AngularJS取决于控制器的配置.通过约定插件创建的配置Struts2可以用于Struts MVC框架生成的前端,也可以用于AngularJS MVW框架创建的前端.

How can you combine Struts2 and AngularJS in web application depends on configuration of your controllers. The configuration Struts2 created via convention plugin can be used for frontend produced by Struts MVC framework or for frontend created by AngularJS MVW framework.

如果您想知道Struts2是否是MVC,那么最好阅读将Struts2设置为Front控制器还是MVC .

If you want to know if Struts2 is MVC then better read Is Struts2 a Front controller or MVC.

请参见 MVVM体系结构AngularJS模式.

在第一种情况下,您将Java与 server 一起使用模型并在Java上与模板语言(例如jsp,freemarker,speed等)结合使用.要从视图访问模型对象,您可以使用表达式语言例如EL,OGNL等.

In the first case you use model and view on the server using Java combined with templating language like jsp, freemarker, velocity, etc. To access the model objects from the view you can use expression languages like EL, OGNL, etc.

在第二种情况下,您将使用模型并结合JavaScript和AngularJS在 client 上进行查看.在这种情况下,为了从服务器获取数据,应用程序将使用http客户端和支持的服务,这些服务以JSON格式返回数据.如果在后端使用Struts 2,则应创建可供AngularJS应用程序使用的相应API.

In the second case you use model and view on the client using JavaScript combined with AngularJS. In this case to get data from the server the application is using http client and backed services that return the data in the JSON format. If you use Struts 2 on the backend then you should create the corresponding API which can be used by AngularJS application.

这是一个示例,您如何使用AngularJS中的API.在这种情况下,通过rest插件创建的Struts 2后端.可以在AngularJS应用程序的后端上使用.

This an example how can you use API from AngularJS. In this case the Struts 2 backend created via rest plugin. It can be used on the backend of AngularJS application.

答案中,您可以找到在为AngularJS创建Struts 2后端时如何将约定和rest插件结合在一起的示例应用

In this answer you can find examples how to combine the convention and rest plugin together when creating Struts 2 backend for AngularJS application

这篇关于无法将对象从操作类传递到Jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆