Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring中web.xml中的URL映射不起作用 包名控制器WelcomeController所在的位置 WEB-INF中视图文件所在的文件夹视图表示html和静态jsp 在视图中welcome.jsp 在WebContent web.xml和welcome-servlet.xml中都有_Spring_Spring Mvc - Fatal编程技术网

spring中web.xml中的URL映射不起作用 包名控制器WelcomeController所在的位置 WEB-INF中视图文件所在的文件夹视图表示html和静态jsp 在视图中welcome.jsp 在WebContent web.xml和welcome-servlet.xml中都有

spring中web.xml中的URL映射不起作用 包名控制器WelcomeController所在的位置 WEB-INF中视图文件所在的文件夹视图表示html和静态jsp 在视图中welcome.jsp 在WebContent web.xml和welcome-servlet.xml中都有,spring,spring-mvc,Spring,Spring Mvc,当我映射/但当我更改url模式时,它不起作用,例如/user/*以下url仅适用于/ 错误是 警告:找不到URI为的HTTP请求的映射 名为的DispatcherServlet中的[/SpringPractice/user/welcome] “欢迎” 如果我设置为/,它就工作了。 甚至我检查了控制器没有错误,因为如果没有找到映射,那么它对/pattern不起作用 WEB.XML <servlet> <servlet-name>welcome</servle

当我映射/但当我更改url模式时,它不起作用,例如/user/*以下url仅适用于/

错误是

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

如果我设置为/,它就工作了。 甚至我检查了控制器没有错误,因为如果没有找到映射,那么它对/pattern不起作用

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>
欢迎使用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>

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

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

<servlet-mapping>
    <servlet-name>welcome</servlet-name>
    <url-pattern>/SpringPractice/*</url-pattern>
</servlet-mapping>
通过将
RequestMapping(value=“/user/welcome”)
添加到控制器类的顶部,它下面的所有映射都将以此为基础。如果您知道某个控制器将处理来自“www.mycolsite.com/user/welcome”的所有请求,那就太好了


我希望这有帮助

为什么要投否决票?这个答案有错误吗?我想知道,这样我可以纠正它。
<servlet-mapping>
    <servlet-name>welcome</servlet-name>
    <url-pattern>/SpringPractice/*</url-pattern>
</servlet-mapping>
@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";
    }
}