Gets the remote HTML insert into the local Web page (Java+react implementation) __html

Source: Internet
Author: User

Requirements: HTML generated through the editor is uploaded to the Aliyun server, but the HTML needs to be embedded in another Web page. Environment Front-End: React backend: Java Java code

@RequestMapping (value = "/gethtml", method = Requestmethod.post)
@UnNeedAdminId
@Unauthorize
Public Object gethtml (@RequestBody map<string, string> uri) throws Exception {
    url url = new URL (uri.get ("URL"));
    URLConnection urlconnection = Url.openconnection ();
    HttpURLConnection httpconnection = (httpurlconnection) urlconnection;
    int responsecode = Httpconnection.getresponsecode ();
    String str = "";
    if (Responsecode = = HTTPURLCONNECTION.HTTP_OK) {
        InputStream in = Httpconnection.getinputstream ();
        InputStreamReader ISR = new InputStreamReader (in);
        BufferedReader bufr = new BufferedReader (ISR);
        String Lstr;
        while ((Lstr = Bufr.readline ())!= null) {
            str = str + lstr;
        }
        Bufr.close ();
    } else {
        str = "no content";
    }
    return str;
}
React Code
Export default class Phomelog extends React.component {static defaultprops = {};
    Constructor (props) {super (props);
    This.state = {sitecontent: ""}} Componentwillmount () {Let id = this.props.id;
    Let URL = ' http://xxxxxxx/${id}/xxx.html ';
      Fetch ({host: "XXX", url: ' api/gethtml ', body: {URL}, method: "POST",}). Then (RES) =>{ This.setstate ({siteContent:res.data})})} Createmarkup = (html) => {return {__HTML:HT
  ML};
    };
    Replaces the characters that are escaped in the returned HTML escapechars= (str) => {str = str.replace (/&amp;/g, ' & ');
    str = str.replace (/&lt;/g, ' < ');
    str = str.replace (/&gt;/g, ' > ');
    str = str.replace (/&acute;/g, ' \ ');
    str = str.replace (/&quot;/g, ' "");
    str = str.replace (/&brvbar;/g, ' \\| ');
    str = str.replace (/& #x27;/g, ' \ ');
  return str;
    } render () {let {sitecontent} = this.state; Sitecontent = This.escapecHARs (sitecontent) return (<div> {sitecontent?) <div dangerouslysetinnerhtml={this.createmarkup (sitecontent)}></div>: <div className= "Pusht"
  ; ......</div>} </div>); }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.