Spring MVC - 如何将商品添加到购物车?

时间:2015-12-30 23:08:17

标签: java spring jsp spring-mvc

我正在编写一个非常基本的在线商店应用程序。我在这里有手机类别(2部手机可用),点击其中一个新页面,其中有关于手机显示的信息,在描述下我得到了按钮“添加到购物车”,这是一件事 - 我想要点击按钮后全部有关该手机的信息将转到数据库并在页面“购物车”中同时显示它们。连接到数据库并在IDE中管理数据对我来说没有问题,但“添加到购物车”按钮如何知道我选择了哪个电话(phone1或phone2)?我应该为Cart创建另一个Controller,或者这是另一种解决方案吗?

PhoneController:

@Controller
@RequestMapping
public class PhoneController {
DBConnection db = new DBConnection();
Cart cart;

@RequestMapping(value="/phone1.html", method = RequestMethod.GET)
public ModelAndView phone1Page() {

    ModelAndView phone1 = new ModelAndView("Phone1");

    return phone1; 

}

@RequestMapping(value="/phone2.html", method = RequestMethod.GET)
public ModelAndView phone2Page() {

    ModelAndView phone2 = new ModelAndView("Phone2");

    return phone2; 

}


@RequestMapping(value="/cart.html", method = RequestMethod.POST)
public ModelAndView addToChart(){


    ModelAndView cart = new ModelAndView("Cart");
    return cart;    

}
}

Phone1.jsp:

<form action="/OnlineShop/cart.html" method="post">
<div style="padding-right: 40px">
    <table border="1">
        <tr>
            <td>Name</td>
            <td>iPhone 6</td>
        </tr>
        <tr>
            <td>Company</td>
            <td>Apple</td>
        </tr>
        <tr>
            <td>Type</td>
            <td>Phone</td>
        </tr>
        <tr>
            <td>Price</td>
            <td>400$</td>
        </tr>
    </table>
     <input type="submit" value="Add to Cart"/>
</div>
</form> 

Cart.jsp:

<div style="padding-right: 40px">
    <table border="1">
        <tr>
            <td>ID</td>
            <td>Product</td>
            <td>Name</td>
            <td>Company</td>
            <td>Type</td>
            <td>Price</td>
            <td>Action</td>
        </tr>
        <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        </tr>

    </table>
</div>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>OnlineShop</display-name>

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

和spring-dispatcher-servlet.xml:

<context:component-scan base-package="com.damian.controller" />
<mvc:annotation-driven/>


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>

</bean>

2 个答案:

答案 0 :(得分:1)

使用请求参数向POST映射发出/cart.html个请求,例如:/cart.html?selected=iPhone6。或者,您可以使用有关所选手机的信息将表单数据添加到您的帖子请求中。我假设您的电话也在具有某种标识符的数据库中,如果您在表单中隐藏此标识符,则可以轻松查看选择了哪个设备。

这是解决问题的一种方法。

<form action="/OnlineShop/cart.html?selectedPhone=iPhone6" method="post">
    <div style="padding-right: 40px">
        <table border="1">
            <tr>
                <td>Name</td>
                <td>iPhone 6</td>
            </tr>
            <tr>
                <td>Company</td>
                <td>Apple</td>
            </tr>
            <tr>
                <td>Type</td>
                <td>Phone</td>
            </tr>
            <tr>
                <td>Price</td>
                <td>400$</td>
            </tr>
        </table>
         <input type="submit" value="Add to Cart"/>
    </div>
</form> 

并检查控制器中的请求参数:

@RequestMapping(value="/cart.html", method = RequestMethod.POST)
public ModelAndView addToChart(@RequestParam String selectedPhone){

    ModelAndView cart = new ModelAndView(selectedPhone);
    return cart;    
}

答案 1 :(得分:1)

如果您希望有关手机的所有信息都会转到数据库并在页面cart中同时显示,您可以尝试添加commandName,如下所示: 但在你为ex:named dto

创建一个PhoneClass类之前
 class public PhoneClass{
  private name ...;
  private ...
  // and 
  setters and getters
  }

 <form action="/OnlineShop/cart.html" method="post" commandName="phoneInfo">
 <div style="padding-right: 40px">
  <table border="1">
    <tr>
        <td>Name</td>
        <td>iPhone 6</td>
    </tr>
   ...
  <input type="submit" value="Add to Cart"/>
 </div>
 </form> 

在您的控制器中,您可以这样做:

    @RequestMapping(value="/cart.html", method = RequestMethod.POST)
    public String someAction(@ModelAttribute("phoneInfo") PhoneClass phoneInfo, Model model) {
    // TODO save un database attributes of `PhoneClass`
    model.addAttribute("phoneInfo", phoneInfo);

    return "somepage";
    }

在您的网页Cart.jsp中,您可以使用jstl的foreach从atrribute phoneInfo中检索数据并将其放入Cart.jsp中,例如:

  <div style="padding-right: 40px">
<table border="1">
    <tr>
   <c:forEach var="element" items="${phoneInfo}" varStatus="status">
    <td> Name</td> <td>${status.name}</td>
        ....
      </c:forEach>

  </div>