Struts 2 - 使用嵌入式URL发送邮件 [英] Struts 2 - Sending mail with embedded URL

查看:260
本文介绍了Struts 2 - 使用嵌入式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=xxxxxyyyyaaaaaa )。

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 ,但是在 determinActionUrl 调用中,我得到一个空指针异常。也许我使用它错误的方式。

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);


推荐答案

你需要创建属性(依赖关系)动作

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发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆