java8 + idea2016 + spring mvc4.3 + maven3.3 + tomcat8 + ubuntu
访问localhost:8080/HelloWeb/hello
或者localhost:8080/hello
都会返回404的状态码:
Web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Form Handling</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-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="org.neo.sml"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
HelloController
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value="/hello")
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
我猜测原因是spring mvc没有识别@Controller
注解,但是我在HelloWeb-servlet.xml
中已经声明了<context:component-scan base-package="org.neo.sml"/>
,请问发生错误的原因可能是什么?
按照@傅易君的提示,参考so上面,我加入了<mvc:annotation-driven />
,但是还是不行:
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.ziwenxie.sml"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
HelloWeb-servlet.xml을 리소스에 넣고 web.xml을
으로 변경합니다. 으아악<servlet>
HelloWeb-servlet.xml 구성은 <context:comComponent-scan base-package="org.neo.sml"/>를 선언합니다.
액세스 경로는 http://localhost:8080/sml/hello입니다. 바로 그거야
컨트롤러 전원을 끄고 컨트롤러에 없는지 확인하시나요?
localhost:8080/sml을 사용해 보세요
먼저 주석 지원 활성화:
으아악주로 주의할 점은 다음과 같습니다.
으아아아1. HelloWeb이 실행되도록 하기 위해 web.xml 파일에서 xml 파일의 위치를 구성할지 여부. -servlet.xml은 src 디렉토리에 있으며 구성은 다음과 같습니다:
2. HelloWeb-servlet.xml 파일에 아래와 같이 구성할지 여부:
으아아아3. 프로젝트가 시작된 디렉터리가 올바른지, 프로젝트 이름을 추가해야 하는지 여부
4. 디렉토리가 정확합니까
이 길을 잃은 것은 아닌 것 같은데요? 왜
으아악HelloWeb
대신sml
을 사용하나요? 보통 프로젝트 이름이 아닌가요? 기본값은 입니다.또는
tomcat的server.xml
을 찾으면 다음 구성을 찾으세요.