简体   繁体   中英

String to Link in Server Side Java Code

我有一个字符串,我想转换为链接,但是我却不能做同样的事情,我的代码如下:

Content.append("<a href=\""+System.getProperty("application.middleware.webapplication.host")).append(":")"/"/">); 

what about a simple solution like this ?

String host = System.getProperty("application.middleware.webapplication.host");
String url = "http://" + host;
String linkText = "please click here";
Content.append("<a href='"+ url + "'>" + linkText + "</a>" ); 

The above doesn't compile. If you didn't try to put everything on one line, you would understand why more easily

Start by creating a variable for System.getProperty("...") . Then put one instruction per line. Then don't mix append() and the + concatenation operator. The code becomes:

String host = System.getProperty("application.middleware.webapplication.host");
content.append("<a href=\"");
content.append(host);
content.append(":")"/"/">);

And the last instruction isn't valid. To become valid, and make it a link, you would need something like

String host = System.getProperty("application.middleware.webapplication.host");
content.append("<a href=\"");
content.append(host);
content.append("\">Click here</a>");

Respecting the Java naming conventions (variables start with a lowercase letter) is also crucial in making code readable and understandable.

Content.append("<a href=\"")
       .append(System.getProperty("application.middleware.webapplication.host"))
       .append("\">My Link</a>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM