动态地将域名传递到Spring JPA中的a href链接中。

huangapple go评论74阅读模式
英文:

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:

 &quot;\n&quot; +

 &quot;&lt;a href=&#39;{domain-name}/payments/&quot;+hashlink+&quot;&#39; target=&#39;_blank&#39;&gt;Click Here&lt;/a&gt;&quot;+                       

&quot;\n&quot; +

This approach above is not working but when I pass the link manually, the "click here" link works:

  &quot;&lt;a href=&#39;http://127.0.0.1:4200/payments/&quot;+hashlink+&quot;&#39; target=&#39;_blank&#39;&gt;Click Here&lt;/a&gt;&quot;+   

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(&quot;PAYMENT NOTIFICATION&quot;,

                        &quot;&lt;p&gt;Dear sir/ma,&lt;/p&gt;\n&quot; +
                                &quot;\n&quot; +
                                &quot;\n&quot; +
                                &quot;&lt;h4&gt;LETTER OF NOTICE &lt;/h4&gt;\n&quot; +
                                &quot;\n&quot; +
                                &quot;\n&quot; +
                                &quot;&lt;p&gt;This is to notify you of the status of your organisation ..... &lt;/p&gt;\n&quot; +
                                &quot;&lt;p&gt;You are hereby put on notice to remit the outstanding amount on or before ....&lt;/p&gt;\n&quot; +
                                &quot;&lt;p&gt;Kindly follow the link below to make....\n&lt;/p&gt;\n&quot; +
                                &quot;\n&quot; +
                                &quot;&lt;a href=&#39;{domain-name}/payments/&quot;+hashlink+&quot;&#39; target=&#39;_blank&#39;&gt;Click Here&lt;/a&gt;&quot;+  
                                &quot;\n&quot; +
                                &quot;\n&quot; +
                                &quot;&lt;p&gt;Thank you.&lt;/p&gt; \n&quot; +
                                &quot;\n&quot;
                        , org.getEmail());
            } catch (
                    MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                System.out.println(&quot;Email Service error&quot;);
                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:

  1. Read/Inject the property to a String variable using @Value

    @Value(&quot;${domain-name}&quot;)
    String domainName;

  2. 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(&quot;${domain-name}&quot;)
    String domainName; // or use constructor injection

    @GetMapping(&quot;a&quot;)
    void a() {
        String hashlink = &quot;jsdklfsdklflsdf&quot;;
        String html = &quot;&lt;p&gt;&quot; +
                &quot;&lt;a href=&#39;&quot; + domainName + &quot;/payments/&quot; + hashlink + &quot;&#39; target=&#39;_blank&#39;&gt;Click Here&lt;/a&gt;&quot; +
                &quot;&lt;/p&gt;&quot;;
        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>



huangapple
  • 本文由 发表于 2020年9月24日 05:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64036792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定