2010-08-25 106 views
0

我有一個JSP與超鏈接Web Flow的問題 - Spring MVC的註釋

<table> 
<tr> 
    <td>Product Name : </td> 
    <td>${product.name}</td> 
</tr> 
<tr> 
    <td>Description:</td> 
    <td>${product.description}</td> 
</tr> 
<tr> 
    <td>Price:</td> 
    <td>${product.price}</td> 
</tr> 
<tr><td>&nbsp;</td></tr>   
<tr> 
    <td> 
     <a href="../shopbasket/populateBasket?code=${product.productCode}&name=${categoryName}">Add to shopping basket</a>        
    </td> 
</tr> 
<tr><td>&nbsp;</td></tr> 
<tr><td>&nbsp;</td></tr>              
<tr> 
    <td> 
    <table> 
     <tr> 
      <td><a href="<c:url value="../index.html"/>">Return to Home Page</a></td> 
      <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> 
      <td><a href="<c:url value="/j_spring_security_logout"/>">Logout</a> 
       (<security:authentication property="principal.username" />) 
      </td> 
     </tr> 
    </table> 
    </td> 
</tr> 

而且控制器

@控制器 @SessionAttributes(「籃子」) 公共類ShopBasketController {

private BasketManager basketManager; 
private CustomerManager customerManager; 
private CategoryManager categoryManager; 

@Autowired 
public ShopBasketController(BasketManager basketManager, CustomerManager customerManager, CategoryManager categoryManager) { 
    this.basketManager = basketManager; 
    this.customerManager = customerManager; 
    this.categoryManager = categoryManager; 
} 

@RequestMapping(value="/basketItems", method=RequestMethod.POST) 
public String removeProduct(@ModelAttribute("basket") Basket basket, BindingResult bindingResult, Model model) {   
    Basket newBasket = ShoppingBasketUtils.removeFromBasket(basket, basketManager); 
    basketManager.update(newBasket); 
    model.addAttribute("basket",newBasket); 
    model.addAttribute("customer", "Sonx"+" Nkuks"); 
    model.addAttribute("totalItems", basketManager.getTotalNumberOfItems(basket)); 
    model.addAttribute("totalPrice", ShoppingBasketUtils.currencyFormat(basketManager.getTotalProductPrice(basket)));  
    return "basketItems"; 
} 

@RequestMapping("/populateBasket") 
public String populateBasket(@RequestParam("code") String productCode, @RequestParam("name") String categoryName, Model model) { 
    Customer customer = customerManager.getCustomer("Sonx", "Nkuks"); 

    if(customer != null) { 
     Basket shopBasket = ShoppingBasketUtils.addToBasket(productCode, categoryManager.getCategory(categoryName), 
       basketManager.getBasket(customer.getReferenceNumber()), basketManager);  
     basketManager.update(shopBasket); 
     model.addAttribute("basket",shopBasket); 
     model.addAttribute("customer", customer.getFirstName()+" "+customer.getLastName()); 
     model.addAttribute("totalItems", basketManager.getTotalNumberOfItems(shopBasket)); 
     model.addAttribute("totalPrice", ShoppingBasketUtils.currencyFormat(basketManager.getTotalProductPrice(shopBasket)));  
     return "basketItems"; 
    } 

    model.addAttribute("customer", "test".concat(" test")); 
    return "/error";    
} 

}

然後形式...

<form:form commandName="basket"> 
<table> 
    <tr> 
     <td> 
     <table> 
      <tr> 
       <td>Customer Name : </td> 
       <td>${customer}</td> 
      </tr> 
     </table> 
     </td> 
    </tr> 
    <tr> 
     <td> 
     <table width="600" border="1" cellspacing="0" cellpadding="2" 
      border="0"> 
      <thead> 
       <tr> 
        <td>Products:</td> 
       </tr> 
       <tr> 
        <td>Product Name</td> 
        <td>Product Code</td> 
        <td>Description</td> 
        <td>Price</td> 
        <td>Remove</td> 
       </tr> 
      </thead> 
      <tbody> 
       <c:forEach items="${basket.products}" var="product"> 
        <tr> 
         <td>${product.name}</td> 
         <td>${product.productCode}</td> 
         <td>${product.description}</td> 
         <td>${product.price}</td> 
         <td><form:checkbox path="removeItemCodes" value="${product.productCode}" /></td> 
        </tr> 
       </c:forEach> 
      </tbody>    
     </table> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <table> 
       <tr> 
        <td>Total Price</td> 
        <td>&nbsp;</td>        
        <td>${totalPrice}</td> 
       </tr> 
       <tr> 
        <td>Total Items</td> 
        <td>&nbsp;</td>      
        <td>${totalItems}</td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
    <tr><td>&nbsp;</td></tr>   
    <tr> 
     <td><input type="submit" value="Remove Items" /></td> 
    </tr> 
    <tr><td>&nbsp;</td></tr> 
    <tr><td>&nbsp;</td></tr>              
    <tr> 
     <td> 
     <table> 
      <tr> 
       <td><a href="<c:url value="../index.html"/>">Return to Home Page</a></td> 
       <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> 
       <td><a href="<c:url value="/j_spring_security_logout"/>">Logout</a> 
        (<security:authentication property="principal.username" />) 
       </td> 
      </tr> 
     </table> 
     </td> 
    </tr> 
</table> 

當我按從第一JSP 「」 控制器成功地執行該方法populateBasket鏈接和加載表。但是,當我提交表單時,我希望它調用POST方法(basketItems)...但它不,按下提交按鈕總是執行GET方法(populateBasket)..這不會發生,如果我加載直接從索引頁形成,它會成功加載。問題是何時來自該JSP?

回答

0

如果您希望表單提交到用於檢索頁面的不同網址,則需要在其上明確設置action。否則,Spring將會使用當前URL填充它。