springMVC第一课--配置文件

时间:2023-03-09 18:21:45
springMVC第一课--配置文件

刚学springMVC,记录下学习过程,供以后查阅(githup源码)。

1,新建一个web工程。(其他按常规来)

如下:添加applicationContext.xml,webmvc-servlet.xml,和lib下的jar包。

springMVC第一课--配置文件

2,修改web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
   <display-name>springmvc</display-name>
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:/spring/applicationContext.xml</param-value>
   </context-param>
   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <servlet>
       <servlet-name>webmvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring/webmvc-servlet.xml</param-value>
       </init-param>
   </servlet>
   <servlet-mapping>
       <servlet-name>webmvc</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
 </web-app>

2,修改applicationContext.xml如下:

 <?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:aop="http://www.springframework.org/schema/aop"
     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/aop  http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">
     <!-- 排除springMVC Controller重复扫描 -->
     <context:component-scan base-package="controller">
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>

 </beans>

3,修改webmvc-servlet.xml如下:

 <?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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     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">
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
        <!-- 扫描业务组件,让spring不扫描带有@Service注解的类(留在root-context.xml中扫描@Service注解的类),防止事务失效 -->
        <mvc:annotation-driven/>
        <context:component-scan base-package="controller">
                <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        </context:component-scan>
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
            <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
         <property name="suffix" value=".jsp" />
        </bean>

 </beans>

4,在controller包下新建一个controller类。

核心代码如下:

 @Controller
 public class MainController {
     @RequestMapping("index")
     public Object index(){
         System.out.println("test");
         return "first";
     }

5,部署项目到tomcat,并在浏览器输入:

http://localhost:8080/springmvc/index

springMVC第一课--配置文件

6,至此,springMVC全部配置完成。