Spring 入门(二)创建spring mvc 项目

因为项目比较简单就就粗略的写了~

1.首先创建一个maven web项目

2.在pom 文件中引入相应的sping 依赖
pom :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>demo2</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>demo2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.3.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
     <!-- spring MVC的相关依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.3.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>demo2</finalName>
    <plugins>
      <!-- 配置Tomcat插件 ,用于启动项目 -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <!--如果不设置,则默认为猫的自定义端口,项目路径为http://localhost:默认端口/项目名  -->
        <configuration>
          <!--如果端口号改为8081:那么访问的时候路径的端口就要写成8081,否则404  -->
          <port>8080</port>
          <!-- 如果设置为/,则项目了路径为http://localhost:端口号 /-->
          <!-- 如果设置为/123,则项目了路径为http://localhost:端口号/123 -->
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
      <file.encoding>UTF-8</file.encoding>
    </properties>
</project>

3.在web.xml 创建拦截器 并加载spring 的配置文件
要在web中加载拦截器并初始化配置文件,配置文件的路径默认是在web的根目录 如果想加载rosource中的被指文件则要奖赏classpath*


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:application-context.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

4.sping配置文件中要 加入注解功能

<?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:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-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/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

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

</beans>

5.编写一个测试类

package com.demo2.controller;

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

import javax.xml.ws.RequestWrapper;

@Controller
public class index {
    @ResponseBody
    @RequestMapping("/index")
    public String index(){
        return "hello";
    }
}

运行项目这里写图片描述

项目启动结果:
这里写图片描述
打开浏览器输入http://localhost:8080/index
这里写图片描述

代码地址:https://gitee.com/HuaChiLiaoNaNvHai/spring-mvc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值