在路径变量{}之前附加“ /”时,@ PathVariable给出404错误

时间:2019-05-04 08:26:23

标签: java spring spring-mvc

我正在研究一个简单的Spring MVC代码。我已经使用spring 4.3.6.RELEASE版本设置了项目。我已经使用@Controller设置了控制器“ PageController.java”。我在使用@PathVariable的网址之一中遇到问题。

@RequestMapping(value = "/test2/{greeting}", method = RequestMethod.GET)
    public ModelAndView test2(@PathVariable("greeting") String greeting) {
        if(null == greeting) {
            greeting = "This is the second default message";
        }
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("greeting", greeting);
        return mv;
    }

当我从value =“ / test2 / {greeting}中删除“ /”时,即将其转换为value =” / test2 {greeting}”时,它按预期工作。但是我想在添加更多路径并连接更多模块时附加“ /”。

我正在分享下面的代码。任何帮助都感激不尽。

我可以使用相同的配置使用@RequestParam很好。

PageController.java

@Controller
public class PageController {

    @RequestMapping(value = "/test2/{greeting}", method = RequestMethod.GET)
    public ModelAndView test2(@PathVariable("greeting") String greeting) {
        if(null == greeting) {
            greeting = "This is the second default message";
        }
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("greeting", greeting);
        return mv;
    }

}

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>

  <!-- Configuring the front-controller -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <!-- Configuring so that every request goes through the front controller -->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = 
   "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

   <context:component-scan base-package = "com.anirvana.onlineshopping.controller" />

   <bean id = "viewResolver"
      class = "org.springframework.web.servlet.view.InternalResourceViewResolver">

      <property name = "prefix" value = "WEB-INF/views/" />
      <property name = "suffix" value = ".jsp" />

   </bean>

</beans>

page.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:set var="contextRoot" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Online Shopping</title>
</head>
<body>
    ${contextRoot} says - ${greeting} 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为您需要删除语句 {greeting} 中的 value = "/test2/{greeting}" 并传递 @RequestParam 而不是 @PathVariable