[英]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("...")
. 首先为
System.getProperty("...")
创建一个变量。 Then put one instruction per line. 然后每行放置一条指令。 Then don't mix
append()
and the +
concatenation operator. 然后不要混合使用
append()
和+
串联运算符。 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. 尊重Java命名约定(变量以小写字母开头)对于使代码易于阅读和理解也是至关重要的。
Content.append("<a href=\"")
.append(System.getProperty("application.middleware.webapplication.host"))
.append("\">My Link</a>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.