2
コントローラからのビューでトリガエラー/検証メッセージをより適切に表現する方法を教えてください。現在、私はboolean属性を送信することでこれを行います。たとえば、製品を作成する際に、2つのエラーが考えられます。製品のUPCのフォーマットが無効です。コントローラからエラーが発生したビューを教えてください。 (Spring MVC)
<div id="menu3"
class="tab-pane fade <c:if test="${activeTab == 3}">in active</c:if>">
<div class="container-fluid" style="padding: 2%;">
<div class="row">
<div class="col-md-12"
style="padding-left: 15%; padding-right: 15%;">
<c:if test="${productCreated}">
<div class="alert alert-success fade in">
<a href="#" class="close" data-dismiss="alert"
aria-label="close">×</a> <strong>Success!</strong>
Product has been created!
</div>
</c:if>
<c:if test="${duplicateProduct}">
<div class="alert alert-warning fade in">
<a href="#" class="close" data-dismiss="alert"
aria-label="close">×</a> <strong>Oh no!</strong>
Product with the UPC ${upc} already exists!
</div>
</c:if>
<c:if test="${invalidFormat}">
<div class="alert alert-warning fade in">
<a href="#" class="close" data-dismiss="alert"
aria-label="close">×</a> <strong>Oops!</strong>
Invalid UPC format!
</div>
</c:if>
<form
action="${pageContext.request.contextPath}/manager/createProduct"
method="post">
<div class="form-group">
<label for="Name">Name </label> <input type="text" name="name"
class="form-control" required />
</div>
<div class="form-group">
<label for="UPC">UPC </label> <input type="number" name="upc"
class="form-control" required />
</div>
<div class="form-group">
<div class="form-group">
<label for="category">Category</label> <select
class="form-control" name="category" required>
<option selected disabled value="">SELECT CATEGORY</option>
<c:forEach items="${categories}" var="item">
<option>${item.getName()}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" rows="5" name="description"></textarea>
</div>
<div class="form-group">
<label for="price">Price </label> <input type="number"
name="price" class="form-control" required />
</div>
<div class="form-group">
<label for="stock">Stock </label> <input type="number"
name="stock" class="form-control" required />
</div>
<button type="submit" class="btn btn-primary">Add
product</button>
</form>
</div>
</div>
</div>
</div>
ブールのトリガーを送信するよりも、この他を行うためのよりよいがあります:私はvalidati
@RequestMapping("/createProduct")
public String createProduct(Model model, @RequestParam(value = "name") String name,
@RequestParam(value = "upc") String upc, @RequestParam(value = "category") String categoryName,
@RequestParam(value = "description") String description, @RequestParam(value = "price") BigDecimal price,
@RequestParam(value = "stock") int stock){
model.addAttribute("activeTab", 3);
if(Validator.invalidUpcFormat(upc)){
model.addAttribute("invalidFormat", true); //trigger for invalid format
return "management";
}
Category category = productService.getCategory(categoryName);
Product product = new Product(upc, category, name, description, price);
InventoryProduct inventoryProduct = new InventoryProduct(product, stock);
try {
managerService.add(inventoryProduct);
model.addAttribute("productCreated", true);
} catch (DuplicateProductException e) {
model.addAttribute("upc", upc);
model.addAttribute("duplicateProduct", true); // trigger for duplicate product
}
return "management";
}
そして、ここが私の見解であるともありますか?
要求パラメータを使用しないでください。オブジェクトを使用し、そのオブジェクトにパラメータをバインドし、オブジェクトを検証します。他のすべてはフレームワークによって処理されます。 –
「オブジェクトを使用し、パラメータをバインドしてオブジェクトを検証する」という意味を理解できません。これは私がspring mvcを使ってきた方法です。と私はかなり新しい春と春のMVC – saluyotamazing
これは、ブーリアンよりも良いアプローチです。http://stackoverflow.com/a/22667775/410677 – kuhajeyan