MVC——03 注解方式环境搭建

SpringMVC 环境搭建

  1. 导入 jar
  2. 在 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>
  1. 在 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>
  1. 编写控制器类
@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"; 
	} 
}
发布了167 篇原创文章 · 获赞 15 · 访问量 6178

猜你喜欢

转载自blog.csdn.net/Re_view/article/details/100149732