【问题标题】:URL mapping is not working in web.xml in spring春季的 web.xml 中的 URL 映射不起作用
【发布时间】:2016-10-15 10:14:42
【问题描述】:
  1. WelcomeController 所在的包名称控制器
  2. WEB-INF 中存在视图文件的文件夹视图表示 html 和静态 jsp
  3. 查看welcome.jsp
  4. 在 WebContent 中有 web.xml 和 welcome-servlet.xml

当我映射 / 但是当我更改 url-pattern 时它不起作用,例如/user/* 以下网址仅适用于 /

http://localhost:3000/SpringPractice/user/welcome

错误是

警告:找不到带有 URI 的 HTTP 请求的映射 [/SpringPractice/user/welcome] 在 DispatcherServlet 中带有名称 “欢迎”

如果我将它设置为 /,它就可以工作。 即使我检查了控制器也没有错误,因为如果没有找到映射,那么它不适用于 / 模式。

WEB.XML

<servlet>
    <servlet-name>welcome</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>welcome</servlet-name>
    <url-pattern>/user/*</url-pattern>
  </servlet-mapping>

WelcomeController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WelcomeController {

    @RequestMapping(method=RequestMethod.GET,value="/user/welcome")
    public String GET(ModelMap model){
        //second is the message name
        //3rd is the message
        model.addAttribute("message","GET Method");
        return "welcome";       //we'll always return the name of the view here welcome.jsp e.g. welcome
    }

    @RequestMapping(method=RequestMethod.POST,value="/user/welcome")
    public String POST(ModelMap model){
        model.addAttribute("message","POST Method");
        return "welcome";
    }
}

welcome-servlet.xml

<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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="controller" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/view/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

【问题讨论】:

标签: spring spring-mvc


【解决方案1】:

如果您希望 servlet 作为根映射到 SpringPractice,则需要修改您的 web.xml

将您的 web.xml 更改为如下所示:

<servlet-mapping>
    <servlet-name>welcome</servlet-name>
    <url-pattern>/SpringPractice/*</url-pattern>
</servlet-mapping>

也有可能您使用了错误的端口。 Tomcat(我假设你在这里使用)默认使用端口 8080

网址:http://localhost:8080/SpringPractice/user/welcome

现在应该可以正常工作了


以下不是必需的,可能会有所帮助

此外,如果您愿意,您可以在班级级别使用@RequestMapping

@Controller
@RequstMapping(value="/user/welcome")
public class WelcomeController {

    @RequestMapping(method=RequestMethod.GET, value="")
    public String GET(ModelMap model){
        //second is the message name
        //3rd is the message
        model.addAttribute("message","GET Method");
        return "welcome";       //we'll always return the name of the view here welcome.jsp e.g. welcome
    }

    @RequestMapping(method=RequestMethod.POST, value="")
    public String POST(ModelMap model){
        model.addAttribute("message","POST Method");
        return "welcome";
    }
}

通过将RequestMapping(value="/user/welcome") 添加到控制器类的顶部,它下的所有映射都将使用它作为基础。如果您知道某个控制器将处理来自“www.MyCoolSite.com/user/welcome”的所有请求,那就太好了

我希望这会有所帮助。

【讨论】:

  • 为什么投反对票?这个答案有什么不正确的吗?我想知道,以便我更正它。
猜你喜欢
  • 2014-04-18
  • 2015-12-23
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多