欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

2.搭建配置最简单的spring mvc 工程-基础版

程序员文章站 2022-04-08 19:11:44
目标:用最少的东西,搭建可以运行的最最基础的springMvc登陆校验项目! spring 4 1.首先配置pom.xml引入spring 相关jar, 引用都有注释, 无关的可以暂时不引用。 2.配置web.xml, 这里使用servlet3.1, 配置相关的applicationContext. ......

目标:用最少的东西,搭建可以运行的最最基础的springmvc登陆校验项目!

spring 4

1.首先配置pom.xml引入spring 相关jar, 引用都有注释, 无关的可以暂时不引用。

<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>com.gof</groupid>
  <artifactid>gof-web</artifactid>
  <packaging>war</packaging>
  <version>0.0.1-snapshot</version>
  <name>gof-web</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <springversion>4.3.0.release</springversion>
    <jacksonversion>2.9.3</jacksonversion>
    <mybatisversion>3.4.4</mybatisversion>
    <mybatisspringversion>1.3.0</mybatisspringversion>
    <c3p0version>0.9.5.2</c3p0version>
    <mysqlversion>5.1.38</mysqlversion>
    <slf4jversion>1.7.21</slf4jversion>
    <log4jversion>1.2.17</log4jversion>
    <jstlversion>1.2</jstlversion>
    <servletapiversion>3.1.0</servletapiversion>
    <junitversion>4.12</junitversion>
  </properties>
  <dependencies>
    <!--junit 测试开始-->
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>${junitversion}</version>
      <scope>test</scope>
    </dependency>
    <!--junit 测试结束-->

    <!--slf4j + log4j日志依赖 开始-->
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-api</artifactid>
      <version>${slf4jversion}</version>
    </dependency>
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-log4j12</artifactid>
      <version>${slf4jversion}</version>
    </dependency>
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>${log4jversion}</version>
    </dependency>
    <!--slf4j + log4j日志依赖 结束-->

    <!--javaee web容器提供 开始-->
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>javax.servlet-api</artifactid>
      <version>${servletapiversion}</version>
      <scope>provided</scope>
    </dependency>
    <!--javaee web容器提供 开始-->

    <!--jsp 常用标签库依赖 开始-->
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>jstl</artifactid>
      <version>${jstlversion}</version>
      <scope>runtime</scope>
    </dependency>
    <!--jsp 常用标签库依赖 结束-->

    <!-- springmvc模块依赖 开始-->
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-webmvc</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>com.fasterxml.jackson.core</groupid>
      <artifactid>jackson-databind</artifactid>
      <version>${jacksonversion}</version>
    </dependency>
    <!-- springmvc模块依赖 结束-->

    <!-- mybatis模块依赖 开始-->
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis</artifactid>
      <version>${mybatisversion}</version>
    </dependency>
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis-spring</artifactid>
      <version>${mybatisspringversion}</version>
    </dependency>
    <!-- mybatis模块依赖 结束-->

    <!--c3p0 数据库连接池依赖 开始-->
    <dependency>
      <groupid>com.mchange</groupid>
      <artifactid>c3p0</artifactid>
      <version>${c3p0version}</version>
    </dependency>
    <!--c3p0 数据库连接池依赖 结束-->

    <!--jdbc 驱动依赖 开始 -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>${mysqlversion}</version>
    </dependency>
    <!--jdbc 驱动依赖 结束 -->

    <!--spring常用模块依赖 开始-->
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-aop</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-aspects</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-beans</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context-support</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-core</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-jdbc</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-orm</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-tx</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-test</artifactid>
      <version>${springversion}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-web</artifactid>
      <version>${springversion}</version>
    </dependency>
    <!--spring常用模块依赖结束-->
  </dependencies>

</project>

2.配置web.xml, 这里使用servlet3.1, 配置相关的applicationcontext.xml, 这里是监听spring, 以及引用spring-mvc.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <!-- *** 初始化spring容器开始 *** -->
  <listener>
    <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
  </listener>
  <context-param>
    <param-name>contextconfiglocation</param-name>
    <param-value>classpath:applicationcontext.xml</param-value>
  </context-param>
  <!-- === 初始化spring容器结束 === -->

  <!-- *** 初始化springmvc开始 *** -->
  <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:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- === 初始化springmvc结束 === -->
</web-app>

3. 配置 applicationcontext.xml, 这里仅配置spring组件扫描,后期其他再继续配置

<?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" xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 注解扫描:扫描@component,@service,@repository等注解,指定com.gof -->
    <context:component-scan base-package="com.gof" />
<!--  <context:annotation-config />仅能够在已经在已经注册过(applicationcontext)的bean上面起作用。-->

</beans>

4.配置spring-mvc.xml, 配置了注解,以及指定扫描的包, 以及指定spring mvc返回的文件目录格式等

<?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: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/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- spring mvc注解:@controller,@requestmapping等 -->
    <mvc:annotation-driven/>
    
    <!-- 对进入dispatcherservlet的url进行筛查,如果发现是静态资源的请求,就将该请求转由web应用服务器默认的servlet处理,如果不是静态资源的请求,才由dispatcherservlet继续处理。 -->
    <mvc:default-servlet-handler/>
    
    <!-- 指定spring mvc 返回的文件格式后缀,目录, 也可以不指定,就进行移除-->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
        <property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/>
        <property name="prefix" value="/web-inf/"/>
        <property name="suffix" value=".html"/>
        <property name="contenttype" value="text/html"/>
    </bean>
    
    <!--只扫描@controller注解类,否则影响事务-->
    <context:component-scan base-package="com.gof" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.controller"/>
    </context:component-scan>
   
</beans>

5. 创建user.java 

package com.gof.domain;

import java.io.serializable;

public class user implements serializable{

    private static final long serialversionuid = 1l;
    
    private string userid;
    
    private string username;
    
    private string password;
    
    private string sex;
    
    private int age;

    
    public string getuserid() {
        return userid;
    }

    public void setuserid(string userid) {
        this.userid = userid;
    }

    public string getusername() {
        return username;
    }

    public void setusername(string username) {
        this.username = username;
    }

    public string getpassword() {
        return password;
    }

    public void setpassword(string password) {
        this.password = password;
    }

    public string getsex() {
        return sex;
    }

    public void setsex(string sex) {
        this.sex = sex;
    }

    public int getage() {
        return age;
    }

    public void setage(int age) {
        this.age = age;
    }
    
    
    
}

6.创建接口:iuserservice

package com.gof.service;

import com.gof.domain.user;

public interface iuserservice {
    
    public user getuserbyusername(string username);
    
}

7.创建实现类:userservice, 这里没有使用到数据,所以数据暂时写固定

package com.gof.service;

import java.util.hashmap;
import java.util.map;

import org.springframework.stereotype.component;

import com.gof.domain.user;

@component
public class userservice implements iuserservice{
    /**
     * 取user
     */
    @override
    public user getuserbyusername(string username) {
        map<string, user> usermap = new hashmap<string, user>();
        //假设username唯一
        user usera = new user();
        usera.setuserid("xxafsfdxafdsafds");
        usera.setusername("jihy");
        usera.setpassword("123456");
    
        user userb= new user();
        usera.setuserid("xxafsfdxafdsafds");
        userb.setusername("jihy2");
        userb.setpassword("123456");
        
        usermap.put(usera.getusername(), usera);
        usermap.put(userb.getusername(), userb);
        
        return usermap.get(username);
    }

}

8.控制层:usercontroller.java

package com.gof.controller;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.responsebody;

import com.gof.domain.jsonresponse;
import com.gof.domain.user;
import com.gof.service.iuserservice;

@controller
public class logincontroller {
    @autowired
    private iuserservice userservice;
    /**
     * 登陆主页
     * @return
     */
    @requestmapping(value="/loginindex")
    public string loginindex(){
        
        return "loginindex";
    }
    /**
     * 验证登陆请求
     * @param username
     * @param password
     * @return
     */
    @requestmapping(value="/login")
    @responsebody
    public jsonresponse login(@requestparam string username, @requestparam string password){
        jsonresponse response = new jsonresponse();
        response.setstatus("error");
        user user = userservice.getuserbyusername(username);
        if (user == null) {
            return response;
        }
        if (!password.equals(user.getpassword())) {
            return response;
        }
        response.setstatus("success");
        return response;
    }
}

9.返回对象:jsonresponse.java

package com.gof.domain;

import java.io.serializable;

public class jsonresponse implements serializable{
    
    private static final long serialversionuid = 1l;

    private string status;
    
    private object result;

    public string getstatus() {
        return status;
    }

    public void setstatus(string status) {
        this.status = status;
    }

    public object getresult() {
        return result;
    }

    public void setresult(object result) {
        this.result = result;
    }
    
    
}

10.l登陆主页:loginindex.html

<html>
<head>
<script type="text/javascript" src="./static/js/lib/jquery/jquery-1.8.2.js"></script>
</head>
<body>
<h2>hello world!</h2>

<form name="loginform">
    <input type="input" name="username" id="username" value="jihy"/>
    <input type="password" name="password" id="password" value="123456"/>
    <input type="button" name="提交"/>
</form>
</body>
<script type="text/javascript">
    alert($("form").serialize());
    $(function(){
        $.ajax({
            type:"post",
            url: "./login", 
            data: $("form").serialize(),
            success: function(data){
            alert(json.stringify(data));
          }});
    })
</script>
</html>

11.引用了jquery插件:

2.搭建配置最简单的spring mvc 工程-基础版

 

12:完成, 使用上面代码, 可以运行的项目!

2.搭建配置最简单的spring mvc 工程-基础版