java - 如何在spring-mvc中的同一个jsp页面的同一个 Controller 上创建两个操作

标签 java spring jsp spring-mvc

问题是我想在 Controller 中为同一个 jsp 页面(main.jsp)创建两个操作 第一个操作在重定向到 main.jsp 页面时执行,以显示产品的详细信息,第二个操作与按钮关联。 如何指示spring的wish方法要调用?

Controller :

 @RequestMapping(value = "pages/main", method = RequestMethod.GET)
 public String detailProduct(final Model model, @RequestParam String id) {

    ProductDTO product = productService.getProduct(Long.parseLong(id));
    ProductModel productbean = mapperDozerBean.map(product, ProductModel.class);

    model.addAttribute("detailProduct", productbean);

    return detailView;
}

@RequestMapping(value = "pages/main", method = RequestMethod.GET)
public String addToCaddy(final Model model, @RequestParam String id,String action) {

    ProductDTO product = productService.getProduct(Long.parseLong(id));

    ...

    return caddyView;
}

jsp:main.jsp

...
    <div id="description">
            <h1>${detailProduct.name}</h1>
            <strong id="price">
                <span>previously &pound;299.00</span> ${detailProduct.price}dhs
            </strong>    
            <p>${detailProduct.description}</p> 
            <p>
                <button type="submit" name="addToCaddy" onclick="location.href='/main.do?id=${detailProduct.id}?'" class="continue" value="addToCaddy" >Ajouter au panier</button> ...

最佳答案

从给定页面发出请求时,不必始终使用相同的 URL。

例如,您可以像这样定义 Controller :

@RequestMapping(value = "pages/main/detail", method = RequestMethod.GET)
public String detailProduct(final Model model, @RequestParam String id) {
    ...
}

@RequestMapping(value = "pages/main/addtocaddy", method = RequestMethod.GET)
public String addToCaddy(final Model model, @RequestParam String id,String action) {
   ...
}

然后在 JSP 上的 GET 请求中传递正确的 url。

关于java - 如何在spring-mvc中的同一个jsp页面的同一个 Controller 上创建两个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21485840/

相关文章:

java - Gradle在eclipse中找到错误的maven存储库url

java - Spring MVC REST 映射在 postman 中返回错误 404

java - Spring+HibernateDAO DAO bean 未找到

java - 如何将字符串编码为UTF8,然后在html中正确显示?

java - XSD 中用于验证 XML 的条件

java - 两个双重嵌套的匿名内部类。如何获得一级匿名类(class)成员?

spring - 如何在 Springs MVC 4 中使用 HikariCP 和 JdbcTemplate

java - spring integration中如何并行同步处理?

java - 错误 : Unable to load configuration. - 操作,在 eclipse 上的 apache 上运行 Struts 2 时由 : Action class not found , 引起

java - JSP mysql 连接没有关闭,但是在 Java 类中它会关闭