[英]Cannot Pass Object from Action Class to JSP
我將 Struts 2 與約定插件與 AngularJS 結合使用。 我有一個動作類和 JSP,如下所示:
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 中顯示未顯示。
如何在 Web 應用程序中結合 Struts2 和 AngularJS 取決於控制器的配置。 Struts2通過約定插件創建的配置可以用於Struts MVC框架產生的前端,也可以用於AngularJS MVW框架產生的前端。
如果您想知道 Struts2 是否是 MVC,那么最好閱讀Is Struts2 a Front controller or MVC 。
請參閱使用 AngularJS 的 MVVM 架構模式的示例和描述。
在第一種情況下,您在服務器上使用模型和視圖,使用 Java 結合模板語言,如 jsp、freemarker、velocity 等。要從視圖訪問模型對象,您可以使用表達式語言,如 EL、OGNL 等。
在第二種情況下,您在客戶端使用 JavaScript 和 AngularJS 結合使用模型和視圖。 在這種情況下,為了從服務器獲取數據,應用程序使用 http 客戶端和支持以 JSON 格式返回數據的服務。 如果您在后端使用 Struts 2,那么您應該創建相應的 API 以供 AngularJS 應用程序使用。
這是一個如何使用 AngularJS 的 API 的例子。 在這種情況下,Struts 2 后端是通過 rest 插件創建的。 它可以用於 AngularJS 應用程序的后端。
在此答案中,您可以找到在為 AngularJS 應用程序創建 Struts 2 后端時如何將約定和其余插件組合在一起的示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.