java后台操作html字符串并当作一个页面返回给浏览器


引入依赖包


        org.jsoup
        jsoup
        1.10.3
    
后台代码如下


/**
	 * 操作html字符串
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("WStoHtml")
	public void WStoHtml(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String url = "http://localhost:8082/bim/static/form2/ApplicationFormTable.htm";
		String body = HttpClientUtil.doPost(url);//body为获取的html代码
	    //System.out.println(body);
	    //System.out.println("11111");
	    Document doc = Jsoup.parse(body);
	    Elements es =  doc.select("table");
	    for (Element element : es) {
            element.html("123");//将table的内容替换为123
        }
	    for (Element element : es) {
            System.out.println(element.html());
        }
	    System.out.println(doc.outerHtml());
	    response.setContentType("text/html;charset=utf-8"); 
	    PrintWriter out=response.getWriter();
	    out.println(doc.outerHtml());
	}

你可能感兴趣的:(java)