【发布时间】:2016-02-19 21:43:23
【问题描述】:
我正在开发具有购买产品交易的网络应用程序,并且该交易需要客户数据。所以在那个交易表单中,我添加了一个模态表单供用户添加新的客户数据。
目前这里是这样的:
link to open the add new customer modal form
这里是我的完整控制器类:
@Controller
公共类 InvoiceProductController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
private MsEmployeeService msEmployeeService;
@Autowired
private MsCustomerService msCustomerService;
@Autowired
CustomerFormValidator customerFormValidator;
@Autowired
private MsLocationService msLocationService;
@Autowired
private MsReligionService msReligionService;
// Set a form validator
@InitBinder("msCustomer")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(customerFormValidator);
}
@RequestMapping(value = "/trproduct", method = RequestMethod.GET)
public ModelAndView trproduct(Locale locale, HttpSession session) {
ModelAndView mav = new ModelAndView("entryinvoiceproduct");
MsUser loggedInUser = (MsUser) session.getAttribute("loggedInUser");
List<MsEmployee> msEmployee = msEmployeeService.findEmployeeByJobsAndLocation("BEAUTY CONSULTANT",
(short) loggedInUser.getMsLocation().getId());
List<MsCustomer> msCustomer = msCustomerService.findAll();
mav.addObject("listCustomer", msCustomer);
mav.addObject("beautyConsultant", msEmployee);
mav.addObject("defaultLocation", loggedInUser);
return mav;
}
@RequestMapping(value = "/trproduct", method = RequestMethod.POST)
public String saveCustomer(@ModelAttribute("customerForm") @Validated MsCustomer msCustomer, BindingResult result,
Model model, final RedirectAttributes redirectAttributes) {
logger.debug("saveOrUpdateUser() : {}", msCustomer);
if (result.hasErrors()) {
populateDefaultModel(model);
return "/trproduct";
} else {
// Add message to flash scope
redirectAttributes.addFlashAttribute("css", "success");
if (msCustomer.isNew()) {
redirectAttributes.addFlashAttribute("msg", "User added successfully!");
} else {
redirectAttributes.addFlashAttribute("msg", "User updated successfully!");
}
msCustomerService.persist(msCustomer);
// POST/REDIRECT/GET
return "/trproduct";
// POST/FORWARD/GET
// return "user/list";
}
}
// show add customer form
@RequestMapping( method = RequestMethod.GET)
public String showAddCustomerForm(Model model, HttpSession session) {
logger.debug("showAddCustomerForm()");
MsCustomer msCustomer = new MsCustomer();
MsUser loggedInUser = (MsUser) session.getAttribute("loggedInUser");
msCustomer.setUpdateUserId(loggedInUser.getId());
msCustomer.getMsLocation().setId(loggedInUser.getMsLocation().getId());
model.addAttribute("customerForm", msCustomer);
populateDefaultModel(model);
return "";
}
private void populateDefaultModel(Model model) {
List<MsLocation> msLocation = msLocationService.findAll();
model.addAttribute("locationList", msLocation);
List<MsReligion> msReligion = msReligionService.findAll();
model.addAttribute("religionList", msReligion);
}
}
这里是我的 jsp,在其中调用模态表单:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- CSS -->
<link href='<c:url value="/resources/datatables/datatables.css" />'
rel="stylesheet">
<script src="<c:url value="/resources/datatables/datatables.js" />"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#invoicedtls').DataTable({
"pageLength" : 8,
"lengthMenu" : [ 8, 16, 32, 64, 100 ],
select : true
});
$('#invoicepayments').DataTable({
"pageLength" : 8,
"lengthMenu" : [ 8, 16, 32, 64, 100 ],
select : true
});
$('#existingcustomer').DataTable({
"pageLength" : 8,
"lengthMenu" : [ 8, 16, 32, 64, 100 ],
select : true
});
// $("body").on("click", ".use-address", function() {
// var id = $(this).closest("tr").find(".custid").text();
// var name = $(this).closest("tr").find(".custname").text();
// alert(id + " , " + name);
// });
});
</script>
<div class="panel panel-default">
<div class="panel-heading">
<h4>Invoice Header</h4>
</div>
<div class="panel-body">
<form role="form">
<div class="col-lg-4">
<label>Customer :</label>
<div class="input-group">
<input type="text" class="form-control text-uppercase">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle"
data-toggle="dropdown">
Customer <span class="caret"></span>
</button>
<ul class="dropdown-menu pull-right">
<li><a data-toggle="modal" data-target="#modalNewCustomer">New</a></li>
<li><a data-toggle="modal"
data-target="#modalExistingCustomer">Existing</a></li>
</ul>
</div>
<!-- /btn-group -->
</div>
<!-- /input-group -->
</div>
<div class="col-lg-3">
<label>Beauty Consultant :</label> <select id="disabledSelect"
class="form-control">
<option value="-1">Select a Beauty Consultant</option>
<c:forEach items="${beautyConsultant}" var="bc">
<option value="${bc.id}">${bc.name}</option>
</c:forEach>
</select>
</div>
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div class="form-group pull-right">
<button class="btn btn-primary pull-right">Add New Product</button>
</div>
<h4>Product List</h4>
</div>
<div class="panel-body">
<!-- <table class="table table-striped table-hover"> -->
<table id="invoicedtls" class="display highlight">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Discount Value</th>
<th>Discount Percent</th>
<th>Total</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<div class="form-group pull-right">
<button class="btn btn-primary pull-right">Add New Payment</button>
</div>
<h4>Payment List</h4>
</div>
<div class="panel-body">
<!-- <table class="table table-striped table-hover"> -->
<table id="invoicepayments" class="display highlight">
<thead>
<tr>
<th>Pay With</th>
<th>Bank Name</th>
<th>Cardholder Name</th>
<th>Card No</th>
<th>Card Expired</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<!-- Modal Existing Customer -->
<div class="modal fade" id="modalExistingCustomer" tabindex="-1"
role="dialog" aria-labelledby="modalExistingCustomerLabel">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="modalExistingCustomerLabel">Select
Existing Customer</h4>
</div>
<div class="modal-body">
<!-- <table class="table table-striped table-hover"> -->
<table id="existingcustomer" class="display compact">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Sex</th>
<th>Origin</th>
<th>Religion</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<c:forEach var="customer" items="${listCustomer}">
<tr>
<td class="custid">${customer.id}</td>
<td class="custname">${customer.firstName}</td>
<td>${customer.sex}</td>
<td>${customer.msLocation.name}</td>
<td>${customer.msReligion.name}</td>
<td>
<button class="btn btn-link btn-xs use-address">Use</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left"
data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Select
Customer</button>
</div>
</div>
</div>
</div>
<!-- Modal New Customer -->
<div class="modal fade" id="modalNewCustomer" tabindex="-1"
role="dialog" aria-labelledby="modalNewCustomerLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="modalNewCustomerLabel">Entry New
Customer</h4>
</div>
<div class="modal-body">
<form id="newCustomer" role="form" action="/admin/users/update" method="post">
<input id="id" name="id" placeholder="Id" value="0" type="hidden"
value="0" />
<div class="form-group">
<label for="firstName">First Name:</label> <input id="firstName"
name="firstName" placeholder="First Name" class="form-control"
type="text" value="" />
</div>
<div class="form-group">
<label for="lastName">Last Name:</label> <input id="lastName"
name="lastName" placeholder="Last Name" class="form-control"
type="text" value="" />
</div>
<div class="form-group">
<label for="email">Email:</label> <input id="email" name="email"
placeholder="Email" class="form-control" type="text" value="" />
</div>
<div class="form-group">
<label for="password">Password:</label> <input id="password"
name="password" placeholder="Password" class="form-control"
type="text" value="" />
</div>
<!-- /.modal-content -->
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left"
data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save Customer</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
// For demo to fit into DataTables site builder...
$('#invoicedtls').removeClass('display').addClass(
'table table-striped table-hover');
$('#invoicepayments').removeClass('display').addClass(
'table table-striped table-hover');
$('#existingcustomer').removeClass('display').addClass(
'table table-striped table-hover table-compact');
// Jquery draggable
$('.modal-dialog').draggable({
handle : ".modal-header"
});
</script>
我感到困惑的是,在控制器类中使用模态表单时,我无法分隔 @RequestMapping 值,因为该值将始终为“/trproduct”。在这个控制器类中,我想保存 2 个不同的表(SlInvoiceHdr 和 MsCustomer),但具有相同的 @RequestMapping 值。
我正在为添加客户模式使用相同的 jsp。
我怎样才能在我的控制器中做到这一点?
谢谢
【问题讨论】:
标签: spring-mvc model-view-controller