SSM(SpringMVC+Spring+Mybatis)

2020-12-13 05:28

阅读:325

标签:string   load   apach   http   framework   art   path   tomcat   resources   

一、搭建SpringMVC

  1、在maven项目中导入spring-webmvc依赖

        
      dependency>
          groupId>org.springframeworkgroupId>
          artifactId>spring-webmvcartifactId>
          version>5.1.8.RELEASEversion>
      dependency>    

  2、配置web.xml文件,在web.xml文件中添加如下代码

servlet>
      servlet-name>HelloWebservlet-name>
      servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      servlet-class>
      
      init-param>
            param-name>contextConfigLocationparam-name>
            param-value>classpath:springmvc.xmlparam-value>
      init-param>
      load-on-startup>1load-on-startup>
   servlet>
   servlet-mapping>
      servlet-name>HelloWebservlet-name>
      url-pattern>/url-pattern>
   servlet-mapping>

   3、配置springmvc核心配置文件,在src/main/resources文件下创建一个springmvc.xml文件

    文件名必须与web.xml文件中的classpath:springmvc.xml名一致

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

    
    context:component-scan base-package="com.blb.controller" />

    
    bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        property name="prefix" value="/WEB-INF/jsp/" />
        property name="suffix" value=".jsp" />
    bean>

beans>

  4、编写测试类

  在src/main/java文件下创建com.blb.controller包,在包下创建一个TestController类

package com.blb.controller;

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

@Controller
public class TestController {
    
    //向浏览器返回
    @ResponseBody
    //拦截地址
    @RequestMapping("hello")
    public String test(){
        return "hello world";
    }

}

  5、maven配置tomcat和jdk

build>
    finalName>SSM_TfinalName>
    plugins>  
        
        plugin>  
            groupId>org.apache.maven.pluginsgroupId>
            artifactId>maven-compiler-pluginartifactId>  
            version>3.1version>  
            configuration>  
                source>1.8source>  
                target>1.8target>  
            configuration>  
        plugin>
        
        
        plugin>
            groupId>org.apache.tomcat.mavengroupId>
            artifactId>tomcat7-maven-pluginartifactId>
            version>2.2version>
        plugin>
     plugins>  
  build>

  6、测试,启动maven项目

    访问localhost:8080/项目名/hello

    页面出现hello world 表示springmvc搭建成功

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

  

SSM(SpringMVC+Spring+Mybatis)

标签:string   load   apach   http   framework   art   path   tomcat   resources   

原文地址:https://www.cnblogs.com/sloth-007/p/11142169.html


评论


亲,登录后才可以留言!