基于Maven的Spring+SpringMVC基础入门

一、新建Maven项目

二、添加Spring配置文件:Spring-core.xml
基于Maven的Spring+SpringMVC基础入门_第1张图片

二、添加Spring配置文件:spring-core.xml


<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">

     
    <context:component-scan base-package="com.xingxue" >
     context:component-scan>

beans>

三、添加SpringMVC配置文件:Spring-mvc.xml


<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:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

  
  
  <context:component-scan base-package="com.xingxue.controller"/>      
   <mvc:annotation-driven/>

        
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp">property> 
    bean>  

beans>

四、在web.xml中配置SpringMVC(可见Spring底层是servlet实现的)


    <servlet>
        <servlet-name>actionservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>actionservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

五、在web.xml中配置Spring

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-core.xmlparam-value>
    context-param>

注:

<param-name>contextConfigLocationparam-name>

中,contextConfigLocation名字不能改;

web.xml如下:



<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-core.xmlparam-value>
    context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>actionservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>actionservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

web-app>

六、
新建一个UserController类
基于Maven的Spring+SpringMVC基础入门_第2张图片

package com.xingxue.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping("login.do")
    public String UserLogin(){

        System.out.println("123456");

        return "OK";
    }
}

新建一个index.jsp
基于Maven的Spring+SpringMVC基础入门_第3张图片

注:跳转的连接分别对应UserController的注解,UserLogin方法返回的字符串OK,在spring-mvc.xml中配置的视图解析器

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp">property> 
    bean>  

会自动拼接出跳转的页面。

你可能感兴趣的:(Spring,SpringMVC)