springmvc教程(1)

淺唱寂寞╮ 提交于 2020-02-06 16:11:09

idea搭建springmvc maven项目

jdk:1.8

maven:Bundled (Maven 3)

idea版本:

 

 

 开始搭建第一个springmvc maven项目

1.点击File->New->Project

 

 

 2.选择maven->勾选Create from archetype->选中 maven-archetype-webapp->点击next

 

 

 3.Name:项目名称   Location:本地工作空间   填写后点击next

 

 

 

 

 4.我没下maven所有就用了默认的,如果本地有的话就使用本地的,填好后finish

 

 5.创建好的项目结构如下

 

 

 

5.1.pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>org.example</groupId>  <artifactId>weChat</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>war</packaging>  <name>weChat Maven Webapp</name>  <!-- FIXME change it to the project's website -->  <url>http://www.example.com</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <maven.compiler.source>1.7</maven.compiler.source>    <maven.compiler.target>1.7</maven.compiler.target>    <spring.version>4.2.4.RELEASE</spring.version>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.11</version>      <scope>test</scope>    </dependency>    <!-- Spring -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.aspectj</groupId>      <artifactId>aspectjweaver</artifactId>      <version>1.8.7</version>    </dependency>  </dependencies>  <build>    <finalName>weChat</finalName>    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->      <plugins>        <plugin>          <artifactId>maven-clean-plugin</artifactId>          <version>3.1.0</version>        </plugin>        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->        <plugin>          <artifactId>maven-resources-plugin</artifactId>          <version>3.0.2</version>        </plugin>        <plugin>          <artifactId>maven-compiler-plugin</artifactId>          <version>3.8.0</version>        </plugin>        <plugin>          <artifactId>maven-surefire-plugin</artifactId>          <version>2.22.1</version>        </plugin>        <plugin>          <artifactId>maven-war-plugin</artifactId>          <version>3.2.2</version>        </plugin>        <plugin>          <artifactId>maven-install-plugin</artifactId>          <version>2.5.2</version>        </plugin>        <plugin>          <artifactId>maven-deploy-plugin</artifactId>          <version>2.8.2</version>        </plugin>        <!--Tomcat7插件  -->        <plugin>          <groupId>org.apache.tomcat.maven</groupId>          <artifactId>tomcat7-maven-plugin</artifactId>          <version>2.2</version>          <configuration>            <uriEncoding>UTF-8</uriEncoding>            <path>/weChat</path>            <port>8080</port>          </configuration>        </plugin>      </plugins>    </pluginManagement>  </build></project>
 5.2.web.xml如下:

 

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"         version="3.0">  <display-name>Archetype Created Web Application</display-name>  <servlet>    <servlet-name>springMvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springMvc</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping></web-app>

 6.这个时候你会发现少了目录,选中项目右键->new->Directory

 

 

 

 

 7.这个时候你会发现有这四个你熟悉的目录,我只需要前面两个即可

 8.resource下创建mvc.xml文件,我的如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"         version="3.0">  <display-name>Archetype Created Web Application</display-name>  <servlet>    <servlet-name>springMvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springMvc</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping></web-app>9.创建controller类
package com.test.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController {    @RequestMapping(value = "/test")    public String test(){        return  "test";    }}10.WEB-INF下创建views文件夹后添加test.jsp页面
<html><body><h2>Hello World! weChat</h2></body></html>11.现在的目录如下:

 

 

12.使用Tomcat7插件配置启动项目;点击Add Configuration

 

 13.点击+-》点击Maven

 

 14.Name:项目名称  Working directory:工作空间    Command line:启动命令  填完后点击ok

 

 15.启动成功

 

 16,访问页面

http://localhost:8080/weChat/

 

 17.访问页面http://localhost:8080/weChat/test.do

 

 18.一个springmvc项目就创建成功啦,你也试试吧。。。。。。


   


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!