idea搭建springmvc项目

1、新建maven工程并添加webapp

idea搭建springmvc项目_第1张图片

2、修改一些参数

idea搭建springmvc项目_第2张图片

3、配置maven信息

idea搭建springmvc项目_第3张图片

4、等待自动下载模板

idea搭建springmvc项目_第4张图片

5、添加java代码和一些配置文件存放的文件夹

idea搭建springmvc项目_第5张图片
会提示java/resouce文件夹(按住shift将两个都选上然后回车)自动创建
idea搭建springmvc项目_第6张图片

6、pom.xml添加依赖



<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.0modelVersion>

  <groupId>org.mhygroupId>
  <artifactId>mvcdemoartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>mvcdemo Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.3.10version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>5.3.10version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.3.10version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.2version>
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>RELEASEversion>
      <scope>compilescope>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>2.9.0version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.9.0version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>2.9.0version>
    dependency>
    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.1version>
    dependency>
    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.4version>
    dependency>
	
	<dependency>
	  <groupId>com.sun.jerseygroupId>
	  <artifactId>jersey-coreartifactId>
	  <version>1.18.1version>
	dependency>
	<dependency>
	  <groupId>com.sun.jerseygroupId>
	  <artifactId>jersey-clientartifactId>
	  <version>1.18.1version>
	dependency>
	
	<dependency>
	  <groupId>org.apache.taglibsgroupId>
	  <artifactId>taglibs-standard-specartifactId>
	  <version>1.2.5version>
	dependency>
	<dependency>
	  <groupId>org.apache.taglibsgroupId>
	  <artifactId>taglibs-standard-implartifactId>
	  <version>1.2.5version>
	dependency>

  dependencies>

  <build>
    <finalName>mvcdemofinalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

7、在web->WEB-INT下的web.xml中添加前端控制器(其他的随意)

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 Applicationdisplay-name>

  
  <filter>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
  filter>

  
  <filter>
    <filter-name>encFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
  filter>
  
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    
    
    <url-pattern>/*url-pattern>
  filter-mapping>

  <filter-mapping>
    <filter-name>encFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  
  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-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>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

将鼠标悬停在红色提示处,然后点击创建,选择在resource文件夹下
idea搭建springmvc项目_第7张图片
idea搭建springmvc项目_第8张图片

8、在springmvc.xml文件中添加配置信息


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <context:component-scan base-package="com.mhy"/>

    
    <mvc:annotation-driven/>
    
    
    <mvc:default-servlet-handler />

    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/" />
        
        <property name="suffix" value=".jsp" />
    bean>
    
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	  
	  <property name="maxUploadSize" value="104857600"/>
	  
	  <property name="defaultEncoding" value="utf-8"/>
	bean>

beans>

9、配置tomcat


idea搭建springmvc项目_第9张图片
idea搭建springmvc项目_第10张图片

10、测试

浏览器访问http://localhost:8080/mvcdemo/hello,控制台显示hhh就表示搭建成功
idea搭建springmvc项目_第11张图片

你可能感兴趣的:(练习,intellij-idea,java,spring)