Java学习记录 10.24-10.28

这篇博客记录了Java学习过程中的SSM框架整合,包括SpringMVC配置、MyBatis整合,以及SSM框架的案例。详细介绍了SpringMVC的页面跳转和JSON处理,包括使用Jackson和FastJson进行数据传输。此外,还讲解了SpringMVC的异常处理方法。
摘要由CSDN通过智能技术生成

10.24

SSM框架整合

SSM框架的整合:Spring + SpringMVC + MyBatis + Druid。学习整合的流程、配置文件的编写。最终完成一个Web应用程序的开发。

1 搭建项目环境

1.1 创建项目

创建的是Maven或者Enterprise类型的项目。自动生成项目的目录目录。

1.项目的结构:

  • 项目名称:ssm-integration
  • 项目包结构:com.cy.ssm
  • 分层架构:pojo、mapper、service、controller - MVC分层架构

2.静态资源:webapp目录下进行存放。

  • webapp下创建:css,存放css文件
  • webapp下创建:web,存放html文件
  • webapp下创建:js,存放js文件
  • webapp下创建:img,存放图片文件

3.非静态资源:jsp文件。在WEB-INF下进行存放。

  • WEB-INF目录下的资源是不可以直接通过客户端访问。转发。

  • 通常会在WEB-INF下创建一个目录,用来存放jsp文件。创建pages目录。

1.2 添加SSM依赖

1.根据的JDK版本来调整项目的目标资源和源文件资源的编译版本,最好保持一致。

2.pom.xml导入相关的依赖。

2 Spring配置

Spring框架功能:将对象的创建和维护交给Spring容器来完成。

1.核心配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
       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.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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    
</beans>

 

 

2.建议配置一个日志的输出格式文件:按照指定的格式来打印输出日志的信息。

log4j.rootLogger=DEBUG, Console
# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

3.使用监听器:在项目的模块创建完成后自动取加载Spring和核心配置文件。

<!-- 启动Spring容器:加载applicationContext.xml,保证此配置文件的加载时机比较早 --><context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- 指定Spring的核心配置文件对应的位置 -->
  <param-value>classpath:applicationContext.xml</param-value></context-param>

<listener>
  <!-- 检测到SpringMVC上下文被创建则此监听器会自动执行 -->
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

4.测试Spring模块的功能是否正常。如果能够获取到ApplicationContext对象,表示Spring模块配置完成。

public class TestSpring {
    @Test
    public void testSpring() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // ClassPathXmlApplicationContext
        System.err.println(context.getClass().getSimpleName());
    }
}

 

3 SpringMVC整合

3.1 SpringMVC配置

1.创建SpringMVC的核心配置文件:springmvc-servlet.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: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">

    <!-- 开启包扫描 -->
    <context:component-scan base-package="com.cy.ssm" />

    <!-- 开启注解驱动 -->
    <mvc:annotation-driven />

    <!-- mvc:resources表示忽略静态资源
         mapping:指定匹配规则,使用"**"包含指定目录下所有的资源
         location:表示本地项目哪一个目录下的资源需要被忽略 -->
    <mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />

    <!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 1.前缀设置 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!-- 2.后缀设置-->
        <property name="suffix" value=".jsp" />
    </bean></beans>

2.在web.xml文件中配置前端控制器。配置项目的编码。

<!-- 配置SpringMVC前端控制器--><servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 设置SpringMVC核心配置文件的位置,默认回去WEB-FIN下寻找 -->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc-servlet.xml</para
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值