【发布时间】:2018-05-05 08:14:51
【问题描述】:
我有以下文件:
web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>To do List</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/tk-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
</web-app>
登录控制器:
package de.yellowsub.tk.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class LoginController {
@RequestMapping(value = "/login")
@ResponseBody
public String sayHello() {
return "Hello World!";
}
}
现在,如果我在 URL 中输入 localhost:8080/login,我可以在浏览器中看到“Hello World”,但如果我写 localhost:8080/spring-mvc/login 则看不到
有什么想法吗?
如果有用的话,这里还有 tk-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/bean/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="de.yellowsub" /> //I also tried "de.yellowsub.*"
<mvc:annotation-driven />
</beans>
【问题讨论】:
-
没用...
-
您使用什么来运行应用程序?独立的Tomcat? Tomcat 在你的 IDE 中?其他?对我来说很奇怪 web.xml 没有被处理,你确定,你在排除上下文路径时点击了这个应用程序吗?我已经复制了你的代码,它对我有用 spring-mvc 上下文路径。
-
这是我第一次使用 Spring/Maven/Tomcat,所以我还不了解所有内容。排除内容路径是什么意思?我正在使用 Spring Tool Suite。所以我按下启动 Spring 应用程序,它为我做了一切
-
@VanDeckel 上下文路径我的意思是“spring-mvc”部分,每个请求都应该需要它。您是否单击 STS 中的“在服务器上运行”?或者您是否有机会制作了 Spring Boot 应用程序(在这种情况下,您正在做的许多配置都不需要/使用)?
-
是的,没有 /spring-mvc/ 也可以工作!是的,我做了一个 Spring Boot 应用程序!非常感谢您的帮助!
标签: java spring spring-mvc web.xml url-pattern