1. 程式人生 > >java後臺操作html字串並當作一個頁面返回給瀏覽器

java後臺操作html字串並當作一個頁面返回給瀏覽器

引入依賴包

<dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.10.3</version>
    </dependency>
後臺程式碼如下
/**
	 * 操作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());
	}