英文:
Dynamically pass domain name into an a href link in Spring Jpa
问题
我正在尝试在使用Spring Jpa发送的电子邮件中创建一个动态链接。我在application.properties文件中创建了一个变量,以便在推送到服务器时,我可以轻松地将该变量从localhost更改为实际部署的域名。然而,当我尝试传递域名值时,链接变得不活动。如果您能指导我正确的方向,我将不胜感激。
以下是application.properties的一部分内容:
server.port=8008
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://41.207.248.189:xxxx/xxx
spring.datasource.username=****
spring.datasource.password=****
##GMAIL PROPERTIES
senderEmail = xxxxx
spring.mail.host=xxxx
spring.mail.port=xxx
spring.mail.username=xxxxxx
spring.mail.password=xxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=xxxxxxx
domain-name=localhost:4200
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
以下是我需要将域名传递给的a href片段:
"\n" +
"<a href='{domain-name}/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
"\n" +
上述方法不起作用,但当我手动传递链接时,"点击这里"的链接起作用:
"<a href='http://127.0.0.1:4200/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
我将非常感谢您的帮助。谢谢。
更新
以下是电子邮件正文的HTML,我在其中设置了域名:
try {
emailService.sendEmail("PAYMENT NOTIFICATION",
"<p>Dear sir/ma,</p>\n" +
"\n" +
"\n" +
"<h4>LETTER OF NOTICE </h4>\n" +
"\n" +
"\n" +
"<p>This is to notify you of the status of your organisation ..... </p>\n" +
"<p>You are hereby put on notice to remit the outstanding amount on or before ....</p>\n" +
"<p>Kindly follow the link below to make....\n</p>\n" +
"\n" +
"<a href='{domain-name}/payments/" + hashlink + "' target='_blank'>Click Here</a>" +
"\n" +
"\n" +
"<p>Thank you.</p> \n" +
"\n"
, org.getEmail());
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Email Service error");
e.printStackTrace();
}
我希望这有所帮助……我期待着获得解决方案。谢谢。
英文:
I am trying to create a dynamic link in an email which am sending out using Spring Jpa. I created a variable in my application.properties file so that whenever I am pushing to server I can easily change that variable from localhost to the domain name for live deployment. However, when I try to pass the domain name value, the link becomes inactive. I would appreciate if you could point me in the right direction.
Here is the application.properties snippet below:
server.port=8008
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://41.207.248.189:xxxx/xxx
spring.datasource.username=****
spring.datasource.password=****
##GMAIL PROPERTIES
senderEmail = xxxxx
spring.mail.host=xxxx
spring.mail.port=xxx
spring.mail.username=xxxxxx
spring.mail.password=xxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=xxxxxxx
domain-name=localhost:4200
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
Here is the snippet for the a href I need to pass the domain-name into:
"\n" +
"<a href='{domain-name}/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
"\n" +
This approach above is not working but when I pass the link manually, the "click here" link works:
"<a href='http://127.0.0.1:4200/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
I would appreciate your help. Thanks
UPDATE
Below is the html for the email body where I am setting the domain-name
try {
emailService.sendEmail("PAYMENT NOTIFICATION",
"<p>Dear sir/ma,</p>\n" +
"\n" +
"\n" +
"<h4>LETTER OF NOTICE </h4>\n" +
"\n" +
"\n" +
"<p>This is to notify you of the status of your organisation ..... </p>\n" +
"<p>You are hereby put on notice to remit the outstanding amount on or before ....</p>\n" +
"<p>Kindly follow the link below to make....\n</p>\n" +
"\n" +
"<a href='{domain-name}/payments/"+hashlink+"' target='_blank'>Click Here</a>"+
"\n" +
"\n" +
"<p>Thank you.</p> \n" +
"\n"
, org.getEmail());
} catch (
MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Email Service error");
e.printStackTrace();
}
}
I hope this helps... I look forward to getting a solution. Thanks.
答案1
得分: 1
基于这里的评论/讨论,以下是你需要做的:
1)使用 @Value 读取/注入属性到一个 String 变量中
@Value("${domain-name}")
String domainName;
2)使用这个变量构建你的 href
完整代码:
application.properties
domain-name=www.abc.com
DomainNameApp:
package domainname;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
public class DomainNameApp {
public static void main(String[] args) {
SpringApplication.run(DomainNameApp.class, args);
}
}
@RestController
class Ctrl {
@Value("${domain-name}")
String domainName; // 或者使用构造函数注入
@GetMapping("a")
void a() {
String hashlink = "jsdklfsdklflsdf";
String html = "<p>" +
"<a href='" + domainName + "/payments/" + hashlink + "' target='_blank'>Click Here</a>" +
"</p>";
System.out.println(html);
}
}
英文:
Based on the comments/discussion here's what you need to do:
-
Read/Inject the property to a String variable using @Value
@Value("${domain-name}")
String domainName;
-
Use the variable to construct your href
Full code:
application.properties
domain-name=www.abc.com
DomainNameApp:
package domainname;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
public class DomainNameApp {
public static void main(String[] args) {
SpringApplication.run(DomainNameApp.class, args);
}
}
@RestController
class Ctrl {
@Value("${domain-name}")
String domainName; // or use constructor injection
@GetMapping("a")
void a() {
String hashlink = "jsdklfsdklflsdf";
String html = "<p>" +
"<a href='" + domainName + "/payments/" + hashlink + "' target='_blank'>Click Here</a>" +
"</p>";
System.out.println(html);
}
}
</details>
# 答案2
**得分**: 1
基于@gtiwari333的回答... 我对我的代码进行了一些更正。我决定详细说明我为将来的读者所做的更改。
首先,我在我的`application.properties`文件中添加了`http://`参数,如下所示:
domain-name=http://localhost:4200
接下来,我在我的类顶部添加了`@Value`注解,如下所示:
@Value("${domain-name}")
String domainName;
然后,我能够轻松地将它传递给链接中的`a href`,如下所示:
"
请点击下面的链接进行....\n
\n" +
"\n" +
"点击这里" +
"\n" +
"\n" +
"
谢谢。
\n" +
"\n"
感谢。我要感谢@gtiwari333帮助我完成这个。
<details>
<summary>英文:</summary>
Based on answers by @gtiwari333... I made some corrections to my code. I decided to detail the changes I made for future readers.
First of all, I added http:// to the parameter in my application.properties, as below:
domain-name=http://localhost:4200
next, I added @Value annotation to the top of my class as below:
@Value("${domain-name}")
String domainName;
Then I was able to pass it into the a href link with ease as below:
"<p>Kindly follow the link below to make....\n</p>\n" +
"\n" +
"<a href='" +domainName+ "/payments/payment-make" + link + "' target='_blank'>Click Here</a>" +
"\n" +
"\n" +
"<p>Thank you.</p> \n" +
"\n"
Thanks. I give credit to @gtiwari333 for helping me with this.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论