有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC如何向购物车添加商品?

我正在写一个应用程序,这是一个非常基本的网上商店。我在这里得到了手机类别(2部手机可用),点击其中一部手机后,我会在“描述”下找到“添加到购物车”按钮,我想点击按钮后,所有关于该手机的信息都会进入数据库,同时在“购物车”页面上显示。连接到db和管理IDE中的数据对我来说没有问题,但“添加到购物车”按钮如何知道我选择了哪部手机(phone1或phone2)?我应该为购物车创建另一个控制器,或者这里有另一个解决方案

电话控制器:

@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;    

}
}

电话1。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> 

推车。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>

网络。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>

还有春天。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>

共 (1) 个答案

  1. # 1 楼答案

    使用请求参数对/cart.html映射执行POST请求,例如:/cart.html?selected=iPhone6。或者,您可以将表格数据添加到post请求中,并提供所选手机的相关信息。我假设你的手机也在一个带有某种标识符的数据库中,如果你在表格中隐藏了这个标识符,那么你就可以很容易地看到选择了哪个设备

    这是解决问题的一种方法

    <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;    
    }