Spring控制器-发送html作为响应

Spring控制器-发送html作为响应,spring,spring-mvc,Spring,Spring Mvc,我有一个加载了产品和addtocart链接的jsp页面。我试图在同一页的div中显示购物车。我想发送html作为响应。这就是我所做的。它只返回字符串输出。有人能告诉我该怎么做吗 控制器 @RequestMapping(value="/addtocart{id}", produces = "text/plain;charset=UTF-8") @ResponseBody public String addToCart(@PathVariable("id") int id,

我有一个加载了产品和addtocart链接的jsp页面。我试图在同一页的div中显示购物车。我想发送html作为响应。这就是我所做的。它只返回字符串
输出
。有人能告诉我该怎么做吗

控制器

    @RequestMapping(value="/addtocart{id}",  produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String addToCart(@PathVariable("id") int id, @ModelAttribute("cart") Cart cart,Model model)
    {
        Product product = productService.getProductById(id);
        if (product != null) {
            CartLine line = new CartLine();
            line.setProduct(product);
            line.setQuantity(1);
            productService.updateProduct(product);
        }
        return "<div>output</div>"; 
    }
@RequestMapping(value=“/addtocart{id}”,products=“text/plain;charset=UTF-8”)
@应答器
公共字符串addToCart(@PathVariable(“id”)int-id、@ModelAttribute(“cart”)cart-cart,Model-Model)
{
Product=productService.getProductById(id);
如果(产品!=null){
CartLine line=新的CartLine();
行。设置产品(产品);
行。设置数量(1);
productService.updateProduct(产品);
}
返回“输出”;
}
JSP

<td><a id="demo4" href="addtocart${product.id}">Add To Cart</a> </td>

$('#demo4').click(function() { 

                $.ajax({    
                url : '/addtocart{id}',
                dataType: 'json',
                contentType: "text/html",
                type : 'GET',
                data :{id:id},
                success : function(response) {
                    $('#output').html(response);
                   }
                 }); 
        }); 

<div id="output" style="display:none">
           <h2>Cart Content(s):</h2>
</div>

$('#demo4')。单击(函数(){
$.ajax({
url:“/addtocart{id}”,
数据类型:“json”,
contentType:“文本/html”,
键入:“GET”,
数据:{id:id},
成功:功能(响应){
$('#output').html(响应);
}
}); 
}); 
购物车内容:

您可以使用AJAX进行操作,但仍可以按照斯拉瓦的建议为您的卡返回单独的页面

JSP页面cart.JSP:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- custom html for your cart representation, just an example -->
<div>
  <h1>${cart.headline}</h1>
  <p>${cart.otherProperty}</p>
</div>
通过这种方式,您可以使用AJAX,但仍然可以返回动态html内容(
调整到一个特定的购物车。)

我也喜欢使用视图的方法,甚至在ajax调用中使用单独的页面。尽管如此,只要将您的
products=“text/plain;charset=UTF-8”
更改为products即可

produces = "text/html;charset=UTF-8"

还有许多其他方面看起来是错误的,与SpringMVC无关,所以即使产品已更正,您仍然需要进行一些更正,以获得您期望的结果

  • 我认为您根本没有发送ajax调用。您很可能正在执行完整的浏览器重定向。第一次阅读时,我很困惑“text/plain”和“text/html”对ajax响应的影响,但现在我相信您实际上是通过浏览器重定向的。将此
    更改为类似的
    并在函数末尾添加
    return false
    。这将执行函数,返回将确保不遵循链接

  • 当您这样做的时候,您会注意到ajax调用中的一些问题;首先,
    url:'/addtocart{id}
    应该是
    url:'/addtocart${product.id}

  • 在complete function not in success中捕获您的响应,并将输出作为response.responseText,响应将返回良好,但浏览器将尝试将其解析为json并失败

  • 您的div将保持不可见,您应该添加一些js来切换它

  • 一个SpringMVC发现,您的购物车bean似乎也有一个名为id的属性。因此,应该重命名id path变量,否则将忽略它

这将是一件比工作更接近的事情,如果不是完全的话

<a id="demo4" href="#">Add To Cart</a>

<div id="output"></div>
<script>

    $('#demo4').click(function() {

        $.ajax({
            url : '/addtocart${product.id}',
            dataType: 'json',
            contentType: "text/html",
            type : 'GET',
            data :{id:4},
            complete: function(response) {
                $('#output').html(response.responseText);
            }
        });
        return false;
    });
</script>

为什么不用HTML代码创建JSP页面并返回它呢?我正在学习ajax,想知道如何用这种方法。谢谢你的训练。但是我如何在同一页中显示它呢?可能吗?我不明白你的问题。确定可以在同一页中显示它吗?它就像你在问题中做的那样。您对
/addtocart{id}
执行一个AJAX请求,但它不会自己指定html响应主体,而是返回cart.jsp的内容和自定义卡片元素。例如,
CART 1
如果
CART.headline
“CART 1”
。这比使用自定义html字符串(如您接受的答案中所示)自己指定响应体要常见得多。只是说…是的,我接受你的方式是正确的,只是想知道如何做到这一点。我也应该接受你的答案?你只能接受一个答案,你应该接受最适合你问题的答案。现在它返回“输出”。我还需要做其他的修改吗?事实上,有很多,我现在就用几个指针编辑我的回复。太棒了!你完全正确。我的div是隐形的。为什么会这样?因为style=“display:none”感谢您的大力帮助。我会接受你的回答。最后一个问题是,您是否可以在注释中更新控制器中的pathvariable更改。只是好奇。
<a id="demo4" href="#">Add To Cart</a>

<div id="output"></div>
<script>

    $('#demo4').click(function() {

        $.ajax({
            url : '/addtocart${product.id}',
            dataType: 'json',
            contentType: "text/html",
            type : 'GET',
            data :{id:4},
            complete: function(response) {
                $('#output').html(response.responseText);
            }
        });
        return false;
    });
</script>
@RequestMapping(value="/addtocart{productId}",  produces = "text/plain;charset=UTF-8")
public String addToCart(@PathVariable("productId") int productId, @ModelAttribute("cart") Cart cart,Model model)