In struts 2 there is a struts tag where you can specify an action name and it gives you the url to that action:
<s:url action="action_name" />
I've been looking for a while now to see if it is possible to do this in an Struts2 Action/Interceptor. I found the class that relates to this struts tag I think (org.apache.struts2.components.URL) but can't figure out how to use it.
This is as far as I got but it might not be how to use it (if its possible at all) but any method I call after this just gives me NullPointerExceptions.:
public String intercept(ActionInvocation ai) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
URL url = new URL(ai.getStack(), request, response);
url.setAction("login");
//e.g. url.start(<with stringwriter>);
}
Hoping this can be done as it would save a lot of troube!
Thanks.
EDIT
URL url = new URL(invocation.getStack(), request, response);
url.setActionMapper(new DefaultActionMapper());
String redirectUrl = url.getUrlProvider().determineActionURL("action_name",
invocation.getProxy().getNamespace(), invocation.getProxy().getMethod(),
request, response, request.getParameterMap(), "http", true, true, false, false);
This code does work and gives me a redirect URL but I was wondering if there was a way to get the CURRENT ActionMapper rather than create a new one. I've done a quick google but can't find anything.
Well this is the method in the component class inside struts2 which is creating action URL
protected String determineActionURL(String action, String namespace, String method, HttpServletRequest req, HttpServletResponse res, Map parameters, String scheme,
boolean includeContext, boolean encodeResult, boolean forceAddSchemeHostAndPort, boolean escapeAmp)
{
String finalAction = findString(action);
String finalMethod = method == null ? null : findString(method);
String finalNamespace = determineNamespace(namespace, getStack(), req);
ActionMapping mapping = new ActionMapping(finalAction, finalNamespace, finalMethod, parameters);
String uri = actionMapper.getUriFromActionMapping(mapping);
return UrlHelper.buildUrl(uri, req, res, parameters, scheme, includeContext, encodeResult, forceAddSchemeHostAndPort, escapeAmp);
}
now the question is how we can get various values for this
action=invocation.getAction();
namespace=invocation.getProxy().getNamespace();
methos= invocation.getProxy().getMethod();
similar other values can be find out from ActionIvocation
This is just an idea and i have not applied it myself.Hope it might help you.
Here is how I get the CURRENT ActionMapper rather than create a new one:
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Inject;
#Namespace("MyNamespace")
public class MyAction extends ActionSupport {
private ActionMapper actionMapper;
private UrlHelper urlHelper;
#Inject
public void setActionMapper(ActionMapper mapper) {
this.actionMapper = mapper;
}
#Inject
public void setUrlHelper(UrlHelper urlHelper) {
this.urlHelper = urlHelper;
}
private String getAbsoluteUrl(String actionName, String namespace) {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ActionContext context = ActionContext.getContext();
ActionInvocation invocation = context.getActionInvocation();
URL url = new URL(invocation.getStack(), request, response);
url.setActionMapper(actionMapper);
url.setUrlHelper(urlHelper);
return url.getUrlProvider().determineActionURL( //
actionName, //
namespace, //
"" , /* Method name */
request, response, //
request.getParameterMap(), "http", //
true, true, true, false);
}
// ...
}
Related
I am trying a GetMapping After a PostMappng. Postmapping basically does an Update.
What I am doing is, if the condition satisfies then no need to update but simply redirect to another page. But seem it is not working, It seems a GetRequest is not Possible from a Postrequest but I have read articles and even seen solutions on Stack but dont know why dont they work for me. Any help or hint will be appreciated
#PostMapping(path = "/campaign/services/migrate")
public String migrateCampaigns(
#RequestParam(required = false, value = "campaignCount") int campaignCount,
#RequestParam(required = false, value = "client-email") String clientEmail,
#RequestParam(required = false, value = "campaign-id") List<String> campaignIds,
#RequestParam(required = false, value = "selectAllCampaigns") String selectAllCampaigns,
HttpServletRequest request,
HttpServletResponse httpResponse,
Model model) throws ServletException, IOException {
logger.info("Retrieving source and destination credential from cookies");
String url = null;
if(campaignCount >= 20) {
url = "redirect:/login/oauth/login-with-eloqua";
}
String adminsEmail = contactService.getEmail(siteNameForEmail);
String userEmail = cookieService.retrieveCookieValue(cookieDefinitionUserEmail);
String clientsEmailAddresses = adminsEmail + "," + userEmail;
migrateResponse = migrationService.migrate(campaignIds, request.getCookies(), clientsEmailAddresses);
}
//return migrateResponse;
return url;
}
#GetMapping(path = "/login/oauth/login-with-eloqua")
public String eloquaOauthLoginPage(HttpServletRequest request, Model model, HttpServletResponse response) {
return "login/oauth/login-with-eloqua";
}
There are many ways to get this approach, it's up on to you. I can see you have a class called HttpServletResponse, you can use a method that name isĀ of the class.
Example:
httpSrvltResponse.sendRedirect("/login/oauth/login-with-eloqua")`
I am working on ajax for getting data from my server as back-end i am using java-servlets
Now what issues i am facing is:
i have to call two data for two different work via ajax
So what i am doing currently is creating two servlet class and making two ajax call to both of them
i am writing all my codes in doGet method of one servlet
and via ajax call in url i am giving servlet class name
What i am trying to do
can't i create one servlet and inside it i can make several methods and make ajax call on that servlet class method
what i am doing
Servlet1 code
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String categoryCode, categoryName, quantity,sql,str = null;
Gson gson = new Gson();
LinkedHashMap<Object, Object> lhm = null;
LinkedList<LinkedHashMap<Object, Object>> mainList = new LinkedList<LinkedHashMap<Object, Object>>();
try {
sql = "1";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
lhm = new LinkedHashMap<Object, Object>();
categoryCode = "A101";
categoryName = "drinks";
lhm.put("Category Code", categoryCode);
lhm.put("Category Name", categoryName);
mainList.add(lhm);
str = gson.toJson(mainList);
}
response.setContentType("application/json");
response.getWriter().write(str);
}}
Servlet2 code
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String itemName, itemCode, quantity,sql,str = null;
Gson gson = new Gson();
LinkedHashMap<Object, Object> lhm = null;
LinkedList<LinkedHashMap<Object, Object>> mainList = new LinkedList<LinkedHashMap<Object, Object>>();
try {
sql = "2";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
lhm = new LinkedHashMap<Object, Object>();
itemName = "pepsi";
itemCode = "AA00";
lhm.put("Item Code", itemCode);
lhm.put("Item Name", itemName);
mainList.add(lhm);
str = gson.toJson(mainList);
}
response.setContentType("application/json");
response.getWriter().write(str);
}
and my ajax call code
$.ajax({
async: true,
url : "Servlet1",
method : "GET",
dataType : "json",
contentType: "application/json; charset=utf-8",
success : function(tableValue) {
addTable(tableValue)
}
});
Now i have to get data from my data base and run 2 queries and have to do two different thing with there result,but doing it with creating new-new servlets now dosn't looks good
Can't i create one doGet and inside that two methods or any two methods inside servlet so that both the servlet codes can be written in oneservlet
Note :- i don't have knowledge on spring framework , so i want to do it with help of servlets only
anyone please guide me how can i do that
Thanks in advance
There isn't a good solution for HttpServlet classes, as you can only have one doGet method. A decent workaround would be adding a parameter in your url, for example Servlet1?action=action1 and then implementing logic in your doGet()
method to decide what to do based on that parameter. For example:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
switch(action){
case "action1":
doAction1(request, response); //Method with the logic of your Servlet1 class
break;
case "action2":
doAction2(request, response);
break;
default:
throw new ServletException("Invalid action parameter");
}
}
Edit - this would be your final ajax call:
$.ajax({
async: true,
url : "Servlet1?action=action1", // Here you set the parameter
method : "GET",
dataType : "json",
contentType: "application/json; charset=utf-8",
success : function(tableValue) {
addTable(tableValue)
}
});
Im having some trouble sending diffrenet response based on query string, I have 2 String that suppose to match a single param from the query names serviceType:
private static String restartQuery = "restarts";
private static String dbStatusQuery = "dbStatus";
And my doGet function needs to send response accordingly:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
String requestType = request.getParameter(serviceType);
// Set response content type
response.setContentType("text/html");
if(requestType.equals(restartQuery)){
//handle response for restartQuery
PrintWriter out = response.getWriter();
out.println("response for restart ....");
}else if (requestType.equals(dbStatusQuery)){
//handle response for dbStatusQuery
PrintWriter out = response.getWriter();
out.println("response for db ....");
}
}
The problem is that I get the same response(restart...), I had check the query string from the front-end - system.println(requestType) and they are different for each request, what can I change to make it work? if there is more code needed please comment below.
I have added 2 things based on comments here:
1- added consts to my defitions:
private static final String restartQuery = "restarts";
private static final String dbStatusQuery = "dbStatus";
2- check spaces from the front-end, just use a simple prefix check.
Thanks to all the helpers.
I have a GWT application which URL is .../Organizer.html. I would like to access it also from url .../organizer. So I decided to replace the original HttpServletRequest with the wrapper with overriden getRequestURL() and getRequestURI() methods in a servlet filter.
Filter code:
String[] urlSplit = req.getRequestURL().toString().split("/");
String urlEnd = urlSplit[urlSplit.length -1];
if (urlEnd.equals(ORGANIZER_URL_ALTERNATIVE)){
String newUrl = req.getRequestURL().toString().
replace(ORGANIZER_URL_ALTERNATIVE, ORGANIZER_URL);
String newUri = req.getRequestURI().toString().
replace(ORGANIZER_URL_ALTERNATIVE, ORGANIZER_URL);
request = new ChangeUrlRequest(req, newUrl, newUri);
}
//Just to check if it works
req = (HttpServletRequest) request;
System.out.println(req.getRequestURL());
System.out.println(req.getRequestURI());
chain.doFilter(request, response);
Request wrapper:
class ChangeUrlRequest extends HttpServletRequestWrapper {
private StringBuffer newUrlBuffer;
private String newUri;
public ChangeUrlRequest(HttpServletRequest request,String newUrl, String newUri) {
super(request);
newUrlBuffer = new StringBuffer(newUrl);
this.newUri = newUri;
}
#Override
public StringBuffer getRequestURL() {
return newUrlBuffer;
}
#Override
public String getRequestURI() {
return newUri;
}
}
This works as expected in way that I can use .../organizer and is replaced by .../Organizer.html
The problem I have now is that even if the URL/URI is the same whether I access .../organizer or .../Organizer.html from browser, only the .../Organizer.html works and for .../organizer the Tomcat complains it cannot find the page: HTTP Status 404 - /my-page/Organizer.html
The output of System.out from the code above:
with Organizer.html (works):
URL: http://localhost:8080/my-page/Organizer.html
URI: /my-page/Organizer.html
with organizer (gives above mentiond 404):
URL: http://localhost:8080/my-page/Organizer.html
URI: /my-page/Organizer.html
Do I need to override or change also something else?
If the goal is to be able to access the app by either URL, maybe there is a simpler solution. You could write a servlet accessible at /organizer and forward the request to the HTML page, e.g.:
#WebServlet(urlPatterns="/organizer")
public class OrganizerServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Organizer.html");
dispatcher.forward(request,response);
}
}
Note that you may need to modify the paths for /organizer and /Organizer.html if they are not at the root of your application.
I have developed a custom tag library in Java which I use in my web application.
I am not sure why but my doTag() is not setting up cookie at all. I have cleared my cache and restarted my computer as well. Here is the code:
public class UserVersionOfSite extends EvenSimplerTagSupport {
private static final Log logger = LogFactory.getLog(UserVersionOfSite.class);
private StringWriter sw = new StringWriter();
#Override
public void doTag() throws IOException, JspException {
getJspBody().invoke(sw); //get the tag body and put it in StringWriter object
//get request object to get cookie value
PageContext ctx = (PageContext)getJspContext();
HttpServletRequest httpServletRequest = (HttpServletRequest) ctx.getRequest();
HttpServletResponse httpServletResponse = (HttpServletResponse) ctx.getResponse();
if(httpServletRequest.getParameterMap().containsKey("show_full_site")) {
logger.debug("show_full_site ");
if(!checkIfCookieExists(httpServletRequest)){
Cookie cookie = new Cookie("SHOW_FULL_SITE",httpServletRequest.getParameter("show_full_site"));
cookie.setMaxAge(86400);
httpServletResponse.addCookie(cookie);
//write the tag output
if(!httpServletRequest.getParameter("show_full_site").equalsIgnoreCase("true")){
//write the response
getJspContext().getOut().println(sw.toString());
}
}else{
String cookieValueString = getCookieValue(httpServletRequest.getCookies(),"SHOW_FULL_SITE","false");
if(!cookieValueString.equalsIgnoreCase("true")){
//write the response
getJspContext().getOut().println(sw.toString());
}
}
}
}
#Override
public String getResult() throws IOException {
return "User version of site";
}
public String getCookieValue(Cookie[] cookies,
String cookieName,
String defaultValue) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
public boolean checkIfCookieExists(HttpServletRequest httpServletRequest){
logger.debug("inside checkIfCookieExists()");
boolean cookiePresent = Arrays.asList(httpServletRequest.getCookies()).contains( "SHOW_FULL_SITE" );
return cookiePresent;
}
}
Even I tried adding the code without using if else statements but still no success. Is there any thing critical I am missing?
Any ideas guys??!!! I have checked the browser's setting as well, but there is nothing there which is blocking a creation of cookie!
I realise the horse has probably bolted by the time I'm posting this but, for the benefit of others stumbling across it, I think the problem may be related to the feature of RequestDispatcher highlighted in this question: unable to add a cookie included in JSP via jsp:include
your following line inside checkIfCookieExists() method is wrong:
Arrays.asList(httpServletRequest.getCookies()).contains( "SHOW_FULL_SITE" );
HttpServletRequest.getCookies() returns Cookie[]. You are wrapping it inside a List and checking for a string "SHOW_FULL_SITE" inside this.
Coming back to your question- how do you know cookie is not being set in the HTTP headers? Try using browser plugins like firebug to see the HTTP response headers coming from server. Also set the path of cookie before adding it to response e.g.
Cookie cookie = new Cookie("SHOW_FULL_SITE",httpServletRequest.getParameter("show_full_site"));
cookie.setMaxAge(86400);
cookie.setPath("/");