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

看别人SSM框架搭建Web服务器实现登录功能的改进后实现

程序员文章站 2022-07-15 09:40:20
...

原文章地址:使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)

因为原文章的配置使用后出现一些问题,所以单独改进实现后可行才写下给原文章看到的人提醒作用。我会在原文地下评论地址

原理的话就自己上网百度就好,上班时间为了省事直接上图

改了两部分:

1. web.xml需要吧log日志那部分去掉 

2.springmvc的配置需要加入<mvc:default-servlet-handler/> 

结构如下:

看别人SSM框架搭建Web服务器实现登录功能的改进后实现

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
	id="WebApp_ID" version="3.1">
  	<display-name>grapefruit</display-name>
<!--
	<!–一个server下如果有多个项目,要对webAppRootKey进行配置,让log能将日志写到对应项目根目录下–>
    <context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>grapefruit.root</param-value>
	</context-param>

	<!– 由Sprng载入的Log4j配置文件位置 –>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>

	<!– Spring默认刷新Log4j配置文件的间隔,单位为millisecond –>
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>-->

	<!-- Web 项目 Spring 加载 Log4j 的监听  ?-->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<!-- 加载spring容器配置 -->
	<listener>
		<description>启动spring容器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring-context.xml</param-value>
	</context-param>

	<!-- 防止内存泄漏的监听器 ?-->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<!-- 配置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>
		<!-- 立马启动servlet ?-->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- servlet-mapping配置 -->
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 解决工程编码过滤器 ?-->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

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

	<!-- 设置默认打开页面列表 -->
	<welcome-file-list>
		<!--<welcome-file>index.html</welcome-file>-->
		<welcome-file>login.html</welcome-file>
	</welcome-file-list>

</web-app>

spring-mvc.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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	<!-- 开启注解 -->
	<mvc:annotation-driven />
	<mvc:default-servlet-handler/>

	<!-- 注解Controller扫描器 -->
	<context:component-scan base-package="com.grapefruit.controller"></context:component-scan>

	<!-- 静态资源访问 ?!-->
	<!-- Spring MVC框架自己处理静态资源,也可定义在resources下 -->
	<mvc:resources location="/image/" mapping="/image/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>
	<mvc:resources location="/css/" mapping="/css/**"/>

    <!-- spring管理拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.grapefruit.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

	<!-- 注解功能的默认配置,处理器和映射器 ?-->
	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<!-- 视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views" />
		<property name="suffix" value=".html"></property>
	</bean>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!--json转换器-->
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name = "supportedMediaTypes">
            <list>
                <bean class="org.springframework.http.MediaType">
                    <constructor-arg index="0" value="text"/>
                    <constructor-arg index="1" value="plain"/>
                    <constructor-arg index="2" value="UTF-8"/>
                </bean>
                <bean class="org.springframework.http.MediaType">
                    <constructor-arg index="0" value="*"/>
                    <constructor-arg index="1" value="*"/>
                    <constructor-arg index="2" value="UTF-8"/>
                </bean>
                <bean class="org.springframework.http.MediaType">
                    <constructor-arg index="0" value="text"/>
                    <constructor-arg index="1" value="*"/>
                    <constructor-arg index="2" value="UTF-8"/>
                </bean>
                <bean class="org.springframework.http.MediaType">
                    <constructor-arg index="0" value="application"/>
                    <constructor-arg index="1" value="json"/>
                    <constructor-arg index="2" value="UTF-8"/>
                </bean>
            </list>
        </property>
    </bean>
</beans>

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

<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
	<!--<context:component-scan base-package="com.bigname.grapefruit.service"></context:component-scan>-->
	<context:component-scan base-package="com.grapefruit.*">
		<!-- 不扫描注解为controller的类型 -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

    <bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <!-- classpath 即src根目录 -->
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

	<!-- 配置数据库连接池c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!-- 指定连接数据库的JDBC驱动 -->
        <property name="driverClass">
            <value>${mysql.driver_class}</value>
        </property>
        <!-- 连接数据库所用的URL -->
        <property name="jdbcUrl">
            <value>${mysql.connection.url}</value>
        </property>
        <!-- 连接数据库的用户名 -->
        <property name="user">
            <value>${mysql.connection.username}</value>
        </property>
        <!-- 连接数据库的密码 -->
        <property name="password">
            <value>${mysql.connection.password}</value>
        </property>
        <!-- 设置数据库连接池的最大连接数 -->
        <property name="maxPoolSize">
            <value>${mysql.connection.maxPoolSize}</value>
        </property>
        <!-- 设置数据库连接池的最小连接数 -->
        <property name="minPoolSize">
            <value>${mysql.connection.minPoolSize}</value>
        </property>
        <!-- 设置数据库连接池的初始化连接数 -->
        <property name="initialPoolSize">
            <value>${mysql.connection.initialPoolSize}</value>
        </property>
        <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
        <property name="maxIdleTime">
            <value>${mysql.connection.maxIdleTime}</value>
        </property>
        <!-- 设置不知道啥东西的东西 -->
        <property name="acquireIncrement">
            <value>${mysql.connection.acquireIncrement}</value>
        </property>
    </bean>

    <!-- 配置mybatis,绑定c3p0 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用上面已经配置好的数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- Mybatis的配置文件路径 -->
        <property name="configLocation" value="classpath:mybatis.xml" />
        <!-- mapper配置路径 -->
        <property name="mapperLocations">
            <list>
                <value>classpath:mapper/*.xml</value>
            </list>
        </property>
    </bean>

    <!-- 使用注解来控制事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <!-- 配置Mybatis事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 扫描生成所有dao层 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.grapefruit.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>

pom.xml(已加入前后台对应json包和文件上传包):

<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.grapefruit</groupId>
	<artifactId>ScorePlatform</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>FirstPlatform</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- 测试工具 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>test</scope>
		</dependency>

		<!-- spring-mvc start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.14.RELEASE</version>
		</dependency>
		<!-- spring-mvc end -->

		<!-- spring start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.14.RELEASE</version>
		</dependency>

		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.14.RELEASE</version>
		</dependency>

		<!-- spring end -->

		<!-- mybatis start -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.1</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>

		<!--  分页助手 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.1.2</version>
		</dependency>
		
		<!-- 代理 -->
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>3.2.2</version>
		</dependency>

		<!-- mybatis end -->

		<!-- c3p0 数据库连接池 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.2</version>
		</dependency>

		<!-- 数据库 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.0.4</version>
			<!--<version>6.0.6</version>-->
		</dependency>
		
		<!-- jsp -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>

		<!-- JSONObject对象依赖的jar包 -->
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.9.3</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
			<version>1.0.6</version>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier><!-- 指定jdk版本 -->
		</dependency>

         <!-- base64转换使用包 -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>

        <!-- multipart/form-data上传解析包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!-- json转换包,前后台对应json用 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.4</version>
        </dependency>

	</dependencies>
	<build>
		<finalName>MavenDemo03</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

login:

看别人SSM框架搭建Web服务器实现登录功能的改进后实现

相关标签: SSM 新手入门