maven工具搭建SpringMVC

Posted xiaotong1223

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven工具搭建SpringMVC相关的知识,希望对你有一定的参考价值。

一、新建maven项目,输入组织机构及项目名称

       技术分享图片

       技术分享图片

      技术分享图片

二、在pom.xml文件中引入所需jar包依赖

        刚建好的工程会报错 :找不到HttpServlet类  同样在pom.xml文件中引入即可

       技术分享图片

      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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.zf</groupId>
  <artifactId>springmvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springmvc Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- HttpServlet所需jar包-->
    <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.0.1</version>
         <scope>provided</scope>
    </dependency>
    
    <!--springmvc所需jar包-->
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
       <version>4.1.2.RELEASE</version>
       <scope>compile</scope>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
       <version>4.1.2.RELEASE</version>
       <scope>compile</scope>
     </dependency>
    
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
  </build>
</project>

三、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
 <web-app version="3.0" 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">
 
    <servlet>
        <servlet-name>springmvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 直接进工程的spring文件下找springmvc.xml文件 -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
     </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
         <url-pattern>/</url-pattern>
   </servlet-mapping>
 
 </web-app>

四、建立controller类

@Controller
@RequestMapping("/index")
public class DemoController {
   
     @RequestMapping("/text")
     public String index(){
          System.err.println(123);
          return "index";//会根据springmvc配置的路径找到以index.jsp文件(.jsp也是在springmvc.xml中配置)
     }
}

五、配置springmvc.xml文件

<?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:context="http://www.springframework.org/schema/context"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
     <!-- 配置扫描的包 -->
    <context:component-scan base-package="cn.zf.*" />
 
    <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
    <mvc:annotation-driven />
 
    <!-- 访问静态资源 -->
    <mvc:default-servlet-handler />
    
    <!-- 视图解析器 -->
     <bean
         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/view/"></property> <!--页面都是放在WEB-INF的view文件夹下-->
         <property name="suffix" value=".jsp"></property>           <!--以.jsp后缀-->
     </bean>
     
 </beans>

六、测试jsp文件     

   login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
   <form action="index/text" method="post">
              用户名:<input type="text" name="userName"><br/>&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"/><br/>
             <input type="submit" value="提交"/> 
            <input type="reset" value="重置"/>                  
     </form>
</body>
</html>

 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
     登录成功
</body>
</html>

七、启动tomcat并测试 

       启动

    技术分享图片

   输入 clean tomcat:run

技术分享图片

  访问工程

     技术分享图片

目录结构

 技术分享图片

 

    

 

以上是关于maven工具搭建SpringMVC的主要内容,如果未能解决你的问题,请参考以下文章

使用intellij idea搭建MAVEN+springmvc+mybatis框架

intellij idea下使用Maven搭建SpringMVC

请问eclipse搭建SSM(spring+springmvc+mybatis)一定要用maven来搭建吗

Spring4 mvc+maven 框架搭建

Spring+SpringMVC+MyBatis+Maven框架整合

Maven构建 SpringMVC+Spring+MyBatis 环境整合