<!--设置根上下文配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--注册ContextLoaderListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--注册DispatcherServlet-->
<!--DispatcherServlet会根据Servlet的名字找到一个文件,并基于该文件加载应用上下文。 此处的Servlet的名字为dispatcher,
因此DispatcherServlet会从"/WEB-INF/dispatcher-context.xml"文件中加载其应用上下文-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--将DispatcherServlet映射到"/"-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置自动扫描的包-->
<context:component-scan base-package="com.spittr" />
<!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
applicationContext.xml不用配置
HelloController:
@Controller
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(){
System.out.println("进入了hello方法");
return "hello";
}
}
hello.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Welcome to SpringMvc</h1>
</body>
</html>