Java MVC项目的意外映射错误

Java MVC项目的意外映射错误,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我的应用程序出现映射错误。正如你从代码中看到的,我有 @RequestMapping("/productList") 但当我在我的主页上,将链接Products(for/productList)悬停在浏览器左下角时,url如下 localhost:8080/eMusiscStore/productList;jsessionid=B16DF0DE6E0089AC5F7DBE356181BBB1 因此,有时页面无法显示,而有时请求的页面(/productList)会正常加载 如何确保每次映射都是

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

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

localhost:8080/eMusiscStore/productList;jsessionid=B16DF0DE6E0089AC5F7DBE356181BBB1
因此,有时页面无法显示,而有时请求的页面(/productList)会正常加载

如何确保每次映射都是正确的

我只发布我的HomeController。如果您需要其他文件,请告诉我

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";
    }
}
package.com.controllers;
导入java.io.IOException;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入com.entities.Product;
导入com.servicesapi.ProductService;
@控制器
公共类家庭控制器{
@自动连线
产品服务产品服务;
@请求映射(“/”)
公共字符串home(){
System.out.println(“HomeController主页”);
返回“家”;
}
@请求映射(“/productList”)
公共字符串getProducts(模型){
System.out.println(“HomeController产品列表”);
List products=productService.getAllProducts();
model.addAttribute(“产品”,产品);
返回“产品列表”;
}
@请求映射(“/productList/viewProduct/{productId}”)
公共字符串viewProduct(@PathVariable String productId,模型模型)引发IOException{
Product=productService.getProductById(productId);
模型。添加属性(产品);
返回“查看产品”;
}
}

正如您在描述中提到的,有时您可以呈现productList页面。这可能是一个不好的缓存问题。您可以尝试以匿名模式运行页面,也可以始终清除缓存。另外,为了确保您的rest调用正常工作,请在chrome浏览器中安装postman插件并尝试调用localhost:8080/eMusiscStore/productList;jsessionid=B16DF0DE6E0089AC5F7DBE356181BBB1
如果一切顺利,这将在响应正文中返回productList。您没有提到CRUD操作的类型吗?看起来它是一个@get方法。

您可以尝试将getProducts()url映射更改为“/productLIst/myProduct”或类似的内容。