在 Struts 2 中发送带有嵌入 URL 的邮件

Sending mail with embedded URL in Struts 2(在 Struts 2 中发送带有嵌入 URL 的邮件)

本文介绍了在 Struts 2 中发送带有嵌入 URL 的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我公司的项目,我必须发送包含嵌入式 URL 的电子邮件,系统会提示用户关注这些 URL.

For a project for my company, I have to send emails containing embedded URLs, which the user will be prompted to follow.

例如,一个人在网站上注册,然后 Struts2 应用程序会向该人发送一封电子邮件,其中有一个 URL 以确认订阅.

For example, a person registers on the website, and then the Struts2 application sends an email to that person in which there is a URL to confirm the subscription.

到目前为止,表单提交和发送电子邮件(从操作内部)都可以正常工作.我遇到的问题是我找不到生成要嵌入邮件正文的 URL 的方法.

So far, the form submission, and sending the email (from inside the action), work just fine. The problem on which I'm stuck is that I can't find a way to generate the URL I'd like to embed in the mail body.

我一定是做错了,但我在想类似以下的事情:

I must be doing it the wrong way, but I was thinking about something like what follows:

public String send() throws Exception {
    StringBuffer body = new StringBuffer();

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("id", "xxxxxyyyyyaaaaa");

    body.append("Veuillez vous rendre ici :");
    body.append(UrlManager.getUrlForAction("action", params));

    SendMail sendMail = new SendMail();
    sendMail.send("me@me.fr", "Information", body.toString());

    return SUCCESS;
}

其中会有一个 UrlManager(框架可以提供的东西)和一个方法 getUrlForAction(),它获取一个动作及其参数作为输入和输出一个包含相应 URL 的字符串(如 http://mysite.mycompany.com/confirm?id=xxxxxyyyyyaaaaa).

where there would be a UrlManager (something that could be made available by the framework) with a method getUrlForAction() that gets an action and its parameters as input and that outputs a String containing the corresponding URL (like http://mysite.mycompany.com/confirm?id=xxxxxyyyyyaaaaa).

有没有人对如何做到这一点有任何想法或指示?

Does anyone have any ideas or pointers on how to do that?

我尝试使用 UrlProvider,但在调用 determineActionUrl 时出现空指针异常.可能是我用错了.

I tried using UrlProvider, but I get a null pointer exception on the determineActionUrl call. Maybe I'm using it the wrong way.

HashMap<String,Object> params = new HashMap<String,Object>();
params.put("id", data.getMd5());

UrlProvider up = new ComponentUrlProvider(
                            new Component(ServletActionContext.getValueStack(ServletActionContext.getRequest())),
                            ServletActionContext.getRequest().getParameterMap());
downloadUrl = up.determineActionURL("confirm", "/", "GET",
                                    ServletActionContext.getRequest(),
                                    ServletActionContext.getResponse(),
                                    params,
                                    "http", true, true, true, true);

推荐答案

你需要在你的action中创建属性(依赖)

You need to create the properties (dependencies) in your action

@Inject
public void setActionMapper(ActionMapper actionMapper) {
  this.actionMapper = actionMapper;
}

private UrlHelper urlHelper;

@Inject
public void setUrlHelper(UrlHelper urlHelper) {
  this.urlHelper = urlHelper;
}

然后在动作中你可以写类似的东西

then in the action you could write something like

Map<String, Object> parameters = new HashMap<>();
ActionMapping mapping = new ActionMapping("action", "namespace", "", parameters);
String uri = actionMapper.getUriFromActionMapping(mapping);
String url  = urlHelper.buildUrl(uri, ServletActionContext.getRequest(), ServletActionContext.getResponse(), parameters, "http", true, false, true, false);

这篇关于在 Struts 2 中发送带有嵌入 URL 的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Struts 2 中发送带有嵌入 URL 的邮件

基础教程推荐