• 关于spring整合velocity之后的Jsoup爬数据使用fastjson的一些小问题


    1、导入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.zdf.jsoup</groupId>
      <artifactId>ZdfJsoup</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <dependencies>
      	<dependency>
      		<groupId>org.jsoup</groupId>
      		<artifactId>jsoup</artifactId>
      		<version>1.11.2</version>
      	</dependency>
      	<dependency>
      		<groupId>com.alibaba</groupId>
      		<artifactId>fastjson</artifactId>
      		<version>1.2.45</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-core</artifactId>
      		<version>4.3.8.RELEASE</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-context</artifactId>
      		<version>4.3.8.RELEASE</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-webmvc</artifactId>
      		<version>4.3.8.RELEASE</version>
      	</dependency>
      	<dependency>
      		<groupId>org.springframework</groupId>
      		<artifactId>spring-context-support</artifactId>
      		<version>4.3.8.RELEASE</version>
      	</dependency>
      	<dependency>
      		<groupId>org.apache.velocity</groupId>
      		<artifactId>velocity</artifactId>
      		<version>1.6.3</version>
      	</dependency>
      	<dependency>
      		<groupId>org.apache.velocity</groupId>
      		<artifactId>velocity-tools</artifactId>
      		<version>2.0</version>
      	</dependency>
      	<dependency>
      		<groupId>log4j</groupId>
      		<artifactId>log4j</artifactId>
      		<version>1.2.11</version>
      	</dependency>
      </dependencies>
    </project>
    

      

    2、代码编写

    package com.zdf.jsoup;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.jsoup.Connection;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.alibaba.fastjson.JSON;
    import com.zdf.jsoup.domain.Ticket;
    @Controller
    public class JsoupTest {
    	@RequestMapping("/jsoupTest")
    public @ResponseBody List<Ticket> jsoupTest() {
    	String url = "http://dynamic.12306.cn/otn/board/boardMore";
    		Connection connect = Jsoup.connect(url);
    		try {
    			List<Ticket> arr = new ArrayList<Ticket>();
    			Document document = connect.get();
    			Element table = document.getElementById("table_board_more");
    			Elements tr = table.getElementsByTag("tr");
    			for (Element element : tr) {
    				Element td = element.child(0);
    				if (td.val().equals("上海")) {
    					System.out.println("出发地:"+td.val()+"终点地:"+element.child(1).val());
    					Ticket ticket = new Ticket();
    					ticket.setStartPlace("上海");
    					ticket.setEndPlace(element.child(1).val());
    					arr.add(ticket);
    				}
    			}
    			return arr;
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return null;
    		
    		
    }
    //	跳转页面
    	@RequestMapping("/")
    	public String toVm() {
    		
    		return "velocity/ticket";
    		
    	}
    }
    

      犯的错误:

    1、虽然导入fastjson.jar但是没有在web.xml中配置转换,因此汇报No converter found for return value of type: class java.util.ArrayList

    解决方案:

    <!-- 处理器适配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
    <list>
    <ref bean="mappingFastJsonHttpMessageConverter"/>
    </list>
    </property>
    </bean>
    
    <!-- 视图解析器 -->
    <!-- velocity解析器,替换掉原来的jsp -->
     <bean id="mappingFastJsonHttpMessageConverter"
              class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    

      2、刚开始使用jsp来表示页面却显示的是源码,原因就是自己配置的是velocity的视图解析器,因此才会出现这个错误

    解决方法:使用vm去导入该文件

    两种方式:使用#include(“ticket.jsp”)是当前位置插入整张ticket.jsp页面,根据需要进行调整。

  • 相关阅读:
    [ 黑盒测试方法 ] 错误猜测法
    [ 黑盒测试方法 ] 边界值分析法
    [ 黑盒测试方法 ] 等价类划分法
    [ Python入门教程 ] Python面向对象编程(下)
    [ Python入门教程 ] Python面向对象编程(上)
    [ Python入门教程 ] Python模块定义和使用
    [ Python入门教程 ] Python常用内置函数介绍
    [ Python入门教程 ] Python函数定义和使用
    MyBatis的缓存分析
    微机原理与接口技术总计
  • 原文地址:https://www.cnblogs.com/df1151112630/p/8448012.html
Copyright © 2020-2023  润新知