有 Java 编程相关的问题?

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

MVC项目的java意外映射错误

我的应用程序出现映射错误。正如你从代码中看到的,我有

@RequestMapping("/productList")

但当我在我的主页上,将链接Products(for/productList)悬停在浏览器左下角时,url如下

localhost:8080/eMusiscStore/productList;jsessionid=B16DF0DE6E0089AC5F7DBE356181BBB1

因此,有时页面无法显示,而有时请求的页面(/productList)会正常加载

我能做些什么来确保我的映射每次都是正确的

我只发布我的家庭控制器。如果你需要其他文件,请告诉我

package com.controllers;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entities.Product;
import com.servicesapi.ProductService;

@Controller
public class HomeController {

    @Autowired
    ProductService productService;

    @RequestMapping("/")
    public String home(){
        System.out.println("HomeController home");
        return "home";
    }

    @RequestMapping("/productList")
    public String getProducts(Model model) {
        System.out.println("HomeController productiList");
        List<Product> products = productService.getAllProducts();
        model.addAttribute("products", products);

        return "productList";
    }

    @RequestMapping("/productList/viewProduct/{productId}")
    public String viewProduct(@PathVariable String productId, Model model) throws IOException{

        Product product = productService.getProductById(productId);
        model.addAttribute(product);

        return "viewProduct";
    }
}

共 (1) 个答案

  1. # 1 楼答案

    正如您在描述中提到的,有时您可以呈现productList页面。这可能是一个严重的缓存问题。您可以尝试以匿名模式运行页面,也可以始终清除缓存。此外,还要确保your rest调用正常运行,在chrome浏览器中安装postman插件,并尝试调用localhost:8080/eMusiscStore/productList;jsessionid=B16DF0DE6E0089AC5F7DBE356181BBB1 如果一切顺利,这应该会在回复正文中返回productList。你不是在说积垢的类型吗?看起来这是一个@get方法