2中使用嵌入式URL发送邮件

2中使用嵌入式URL发送邮件

本文介绍了在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("[email protected]", "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);

推荐答案

您需要在操作中创建属性(依赖项)

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

07-23 18:11