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
    评论
您可以使用Matlab中的FFT函数进行谐波分析,基于提供的数据,可以按照以下步骤编写程序: 1. 定义给定数据的采样频率 `fs`,根据数据中的时间间隔计算得出,即 `fs = 1 / (时间间隔)`; 2. 对输入数据进行FFT变换,使用fft函数,得到频域数据 `Y`; 3. 计算基波幅值 `fundamental`,即第一个频率分量的幅值,即 `abs(Y(1))`; 4. 计算2到11次谐波幅值 `harmonics`,即第2到11个频率分量的幅值,即 `abs(Y(2:11))`; 5. 输出结果。 下面是一个可能的Matlab程序: ``` % 定义时间间隔和数据 t = [0 0.000208333 0.000416667 0.000625 0.000833333 0.001041667 0.00125 0.001458333 0.001666667 0.001875 0.002083333 0.002291667 0.0025 0.002708333 0.002916667 0.003125 0.003333333 0.003541667 0.00375 0.003958333 0.004166667 0.004375 0.004583333 0.004791667 0.005 0.005208333 0.005416667 0.005625 0.005833333 0.006041667 0.00625 0.006458333 0.006666667 0.006875 0.007083333 0.007291667 0.0075 0.007708333 0.007916667 0.008125 0.008333333 0.008541667 0.00875 0.008958333 0.009166667 0.009375 0.009583333 0.009791667 0.01 0.010208333 0.010416667 0.010625 0.010833333 0.011041667 0.01125 0.011458333 0.011666667 0.011875 0.012083333 0.012291667 0.0125]; y = [15.92 15.68 15.52 15.04 14.6 14.08 12.96 12.24 10.96 9.76 8.28 6.8 5.56 3.24 1.72 -0.76 -3 -5.24 -6.76 -8.4 -10.24 -11.32 -12.72 -14.4 -15.2 -16.28 -16.6 -17.28 -17.44 -17.24 -17.32 -17.04 -16.84 -16.04 -15.88 -15.16 -14.08 -13.32 -12.04 -10.88 -10.16 -7.96 -6.4 -4.72 -2.36 -0.48 2.08 3.8 5.92 7.32 8.88 10.36 11.64 12.88 13.72 14.84 15.44 15.76 15.96 16.04 15.52]; % 计算采样频率 fs = 1 / (t(2) - t(1)); % 计算FFT变换 Y = fft(y); % 计算基波幅值 fundamental = abs(Y(1)); % 计算2到11次谐波幅值 harmonics = abs(Y(2:11)); % 输出结果 fprintf('基波幅值: %.2f\n', fundamental); fprintf('2到11次谐波幅值: %.2f ', harmonics); fprintf('\n'); ``` 注意:这个程序假设您提供的数据是一个完整的周期数据,如果不是一个完整的周期数据需要进行预处理,例如使用窗函数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值