SpringMVC 环境搭建
- 导入 jar
- 在 web.xml 中配置前端控制器 DispatcherServlet
注:如 果 不 配 置 init-param 标签会 在/WEB-INF/ servlet-name 标签-servlet.xml
<servlet>
<servlet-name>jqk</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jqk</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- 在 src 下新建 springmvc.xml
注:引入 xmlns:mvc 命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.youdian.controller"></context:component-scan>
<!-org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandler Mapping -->
<!-org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA dapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
</beans>
- 编写控制器类
@Controller
public class DemoController {
@RequestMapping("demo")
public String demo(){
System.out.println("执行 demo");
return "main.jsp";
}
@RequestMapping("demo2")
public String demo2(){
System.out.println("demo2");
return "main1.jsp";
}
}