Struts 2 - send mail with embedded URL

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

For example, a person signs up on a website, and then the Struts2 application sends an email to the person who has the URL to confirm the subscription.

So far, submitting the form and sending emails (from within the action) work very well. The problem I'm stuck on is that I cannot find a way to create a URL that I would like to embed in the body of the message.

I should be doing it wrong, but I was thinking of something like the following:

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

where is UrlManager (something that can be accessed by the infrastructure) using the getUrlForAction() method, which receives the action and its parameters as input and displays a string containing the corresponding URL (for example, http://mysite.mycompany.com/confirm?id=xxxxxyyyyyaaaaa ).

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

EDIT:

I tried using UrlProvider , but I get a null pointer exception in the call to determineActionUrl . 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); 
+4
source share
2 answers

You need to create properties (dependencies) in action

 @Inject public void setActionMapper(ActionMapper actionMapper) { this.actionMapper = actionMapper; } private UrlHelper urlHelper; @Inject public void setUrlHelper(UrlHelper urlHelper) { this.urlHelper = urlHelper; } 

then in 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); 
+2
source

You can take a look at UrlsProvider.determineActionURL , which does what you need.

UPDATE Indeed, I needed to use this method a few weeks ago, and I remember how NPE went up around ... In the end, I was able to write the following code (adapted from my application):

 String getMyActionUrl() throws Exception { ActionInvocation invocation = (ActionInvocation) ActionContext .getContext().get(ActionContext.ACTION_INVOCATION); org.apache.struts2.components.URL url = new org.apache.struts2.components.URL(invocation.getStack(), getServletRequest(), getServletResponse()); url.addParameter("parameterToBeDeleted", null); url.addParameter("newParam", newValue); url.setActionMapper(new DefaultActionMapper()); url.setUrlHelper(new DefaultUrlHelper()); String myUrl = url .getUrlProvider() .determineActionURL( "MyActionName", invocation.getProxy().getNamespace(), invocation.getProxy().getMethod(), getServletRequest(), getServletResponse(), getServletRequest().getParameterMap(), "http", true, true, false, false); return myUrl; } 

Notes:

  • MyActionName here as a string is not big code supporting maintanable. Make sure it matches the name you give the action (which may be different from the name of the Action class that handles the calls for this action).

  • http is hardcoded. Change if necessary.

0
source

Source: https://habr.com/ru/post/1489497/


All Articles