<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
详解:主要对和元素进行了配置
中,配置了SpringMVC的前端控制器DispatcherServlet
并通过子元素配置了SpringMVC的配置文件位置
元素的1表示容器在启动时立即加载这个Servlet
元素中 /会将所有URL拦截,并交给DispatcherServlet
public class FirstController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
ModelAndView mv=new ModelAndView();
mv.addObject("msg","冉雨珊?张宇");
mv.setViewName("/index");
return mv;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--视图解析器:DispatcherServlet给他的ModelAndivew-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceView">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/" />
<!--前缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置处理器 -->
<bean name="/firstController" class="com.aqiuo.mc.controller.FirstController"></bean>
</beans>