Java 在Spring Jpa中将域名动态传递到a href链接中

Java 在Spring Jpa中将域名动态传递到a href链接中,java,html,spring,Java,Html,Spring,我试图在一封使用SpringJPA发送的电子邮件中创建一个动态链接。我在application.properties文件中创建了一个变量,这样每当我推送到服务器时,我就可以轻松地将该变量从localhost更改为用于实时部署的域名。但是,当我尝试传递域名值时,链接变为非活动状态。如果你能为我指出正确的方向,我将不胜感激 下面是application.properties代码段: server.port=8008 spring.jpa.database=POSTGRESQL spring.dat

我试图在一封使用SpringJPA发送的电子邮件中创建一个动态链接。我在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" +


“\n”+
""+                       
“\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();
            }
        }



试一试{
emailService.sendEmail(“付款通知”,
“尊敬的先生/女士,

\n”+ “\n”+ “\n”+ “通知函\n”+ “\n”+ “\n”+ “这是为了通知您组织的状态…..

\n”+ “特此通知您在……或之前汇出未付金额”

\n”+ “请按照下面的链接制作….\n

\n”+ “\n”+ ""+ “\n”+ “\n”+ “谢谢。

\n”+ “\n” ,org.getEmail()); }抓住( 信息例外(e){ e、 printStackTrace(); }捕获(例外e){ System.out.println(“电子邮件服务错误”); e、 printStackTrace(); } }

我希望这有助于。。。我期待着得到一个解决方案。谢谢。

根据评论/讨论,以下是您需要做的事情:

  • 使用@Value将属性读取/注入字符串变量
    @Value(“${domain name}”)字符串域名

  • 使用该变量构造href


  • 完整代码:

    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);
        }
    
    }
    
    应用程序属性

    domain name=www.abc.com

    域名应用程序:

    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);
        }
    
    }
    
    包域名;
    导入org.springframework.beans.factory.annotation.Value;
    导入org.springframework.boot.SpringApplication;
    导入org.springframework.boot.autoconfigure.springboot应用程序;
    导入org.springframework.web.bind.annotation.*;
    @SpringBoot应用程序
    公共类域名应用{
    公共静态void main(字符串[]args){
    run(DomainNameApp.class,args);
    }
    }
    @RestController
    类Ctrl{
    @值(“${domain name}”)
    字符串domainName;//或使用构造函数注入
    @GetMapping(“a”)
    作废{
    字符串hashlink=“jsdklfsdklflsdf”;
    字符串html=“”+
    "" +
    “

    ”; System.out.println(html); } }
    根据评论/讨论,您需要做以下几点:

  • 使用@Value将属性读取/注入字符串变量
    @Value(“${domain name}”)字符串域名

  • 使用该变量构造href


  • 完整代码:

    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);
        }
    
    }
    
    应用程序属性

    domain name=www.abc.com

    域名应用程序:

    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);
        }
    
    }
    
    包域名;
    导入org.springframework.beans.factory.annotation.Value;
    导入org.springframework.boot.SpringApplication;
    导入org.springframework.boot.autoconfigure.springboot应用程序;
    导入org.springframework.web.bind.annotation.*;
    @SpringBoot应用程序
    公共类域名应用{
    公共静态void main(字符串[]args){
    run(DomainNameApp.class,args);
    }
    }
    @RestController
    类Ctrl{
    @值(“${domain name}”)
    字符串domainName;//或使用构造函数注入
    @GetMapping(“a”)
    作废{
    字符串hashlink=“jsdklfsdklflsdf”;
    字符串html=“”+
    "" +
    “

    ”; System.out.println(html); } }
    基于@gtiwari333的答案。。。我对代码做了一些修改。我决定为未来的读者详述我所做的改变

    首先,我在application.properties中的参数中添加了http://如下:

    domain-name=http://localhost:4200
    
    @Value("${domain-name}")
        String domainName;
    
    
    
    "<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"
    
    
    接下来,我在类的顶部添加了@Value注释,如下所示:

    domain-name=http://localhost:4200
    
    @Value("${domain-name}")
        String domainName;
    
    
    
    "<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"
    
    
    然后我可以轻松地将其传递到a href链接,如下所示:

    domain-name=http://localhost:4200
    
    @Value("${domain-name}")
        String domainName;
    
    
    
    "<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"
    
    
    “请按照下面的链接制作….\n

    \n”+ “\n”+ "" + “\n”+ “\n”+ “谢谢。

    \n”+ “\n”

    谢谢。我感谢@gtiwari333在这方面的帮助。

    基于@gtiwari333的回答。。。我对代码做了一些修改。我决定为未来的读者详述我所做的改变

    首先,我在application.properties中的参数中添加了http://如下:

    domain-name=http://localhost:4200
    
    @Value("${domain-name}")
        String domainName;
    
    
    
    "<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"
    
    
    接下来,我在类的顶部添加了@Value注释,如下所示:

    domain-name=http://localhost:4200
    
    @Value("${domain-name}")
        String domainName;
    
    
    
    "<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"
    
    
    然后我可以轻松地将其传递到a href链接,如下所示:

    domain-name=http://localhost:4200
    
    @Value("${domain-name}")
        String domainName;
    
    
    
    "<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"
    
    
    “请按照下面的链接制作….\n

    \n”+ “\n”+ "" + “\n”+ “\n”+ “谢谢你