how can I set content length from struts2 action class? as I access my web app from mobile, mobile client browser is complaining that content length required. Only one mobile is giving me headache. Thanks.
If you want set it for specific Action-classes you can get an object of HttpServletResponse and can than set the content length like
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentLength(lenght);
Other good way is to implement ServletResponseAware and let Struts2 inject response as a map in your action class (clean way).
Other good way is to do such work in an interceptor,create your custom interceptor and place it where you want to do some pre-processing and post-processing.
Other way is to create a Filter and place it before Struts2 dispatcher Filter and let your filter do any custom logic you want to have with request and response object.
Related
Can I create a response object in java without creating a Servlet? I have a regular java class right now called Menu. It contains not only my menus for my application but it also contains my CRUD functionality that is tied to the database. Which works fine on the console. What I want to do now is to display the result set on a webpage. I want to use AJAX (no frameworks like jQuery etc.) to capture the information from the result set. In the Menu class I am going to convert the extracted information into a Json string with Jackson databinding (object mapper). I want to use a PrintWriter to send this information back to AJAX. I am assuming I need a response object. Do I have to re-write my Menu class as a Servlet or is there another way to accomplish this?
For querying data from server you need to have a resource which has
http url
can accept a form of request as input
can give response based on business logic in menu crud class.
For this querying as you said you can do with ajax call to this servlet with specified url pattern from your frontend webpage.
In short servlet will be essential. It can handle all http method types like get post and so on.
So you can map your crud methods to your servlet methods
eg :
doGet - querying items.
doPost- creation of item.
doDelete- delete an item.
and so on.
further you will call your crud methods from these servlet do* methods.
for example refer: https://www.geeksforgeeks.org/servlet-crud-operation-with-example/
Hope this helps.
I'am making a web page with a login system and backoffice page. The problem is, both use the method "doPost" (the login use to autenticate and the backoffice use to insert data in db). How can I use only one servlet for both? I'am asking this because both use doPost, so I made two servlet's.
In case you want to use a single servlet, you should implement Front Controller Pattern. For this, you will parse the request URL and decide which action should be performed:
public class MySingleServlet extends Servlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String url = request.getPathInfo();
//returns the action to handle
Action action = ActionFactory.getAction(url);
action.process(request, response);
}
}
This involves an Action interface/abstract class and an ActionFactory that will parse the URL and return the right implementation to handle the actions to do.
Another more naive and harder-to-maintain implementation is by sending an action parameter. This may be a problem because an attacker may use a proxy and change the action parameter before sending the request to the URL. If this is a recognized valid action, and the attacker knows what to send, then you're in trouble.
Note that there are MVC frameworks that already implement Front Controller Pattern like Spring MVC and JSF, so there's no need to reinvent the wheel unless it is for learning purposes (otherwise, you should use a library that already implements this).
You could add an extra parameter (e.g. action) in your post method
retrieved from a hidden form field, if you are using forms, or
added with a simple &action='value' to your request if using xml http request
and based on its value perform the appropriate actions:
if (action.equals("auth"))
{
// authenticate
}
else if (action.equals("backoffice"))
{
// db update
}
You can get pathInfo from request object based on that you route the request.
is their a neat way to pass a model to jsp, render the jsp and return the html as string using Spring. The html is then used in an e-mail that is fired off programmitcally, I do not want to use freemarker, but maybe I should ?
The url being requested is part of the same app.
I want one of my service layer classes to be able to call a view and use the html as a String.
You can call requestDispatcher.include(request, response) method.
You will need to implement the request and response objects. The request object will provide all information to the dispatcher which page should be rendered, the response object you will pass to the call will then capture the result to a string (using e.g. a StringBuilder).
See e.g. this tutorial for more info.
I'm guessing a servlet filter will do the trick? Not really a Spring solution, but easy enough to do.
Also this answer seems relevant, although it is DWR that you may not necessarily want to use in this instance.
You can use Velocity to create an email template:
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "emailTemplate.vm", model);
There is a complete chapter in the Spring reference docs of how Spring can be used to send emails of various types.
My websites pages are broken down into 3 parts:
Header
Main Content
Footer
The header needs to access the user object to display things like the username, sign/signout links depending if the user is logged in or not.
I want my velocity templates to be like this:
inject header template, that will need access to the user object.
main content will be a spring mvc action page
footer
I know that in my spring mvc action, I will simple load my velocity template with the attributes/objects that I will need to display in the 'main content' area.
Is there a way for me to provide the 'header template' access to the attributes/objects it needs from a place OTHER than the spring mvc action?
I am new to java/spring, is there a request level collection that I can load my user object into, so I can reference it from there?
I think that the way to do this in the Spring MVC framework is to use a custom Handler Interceptor that attaches the necessary information to the request (or response) object and then returns true to allow the request to proceed to the designated controller.
(Warning: I've never done this, YMMV, etc ...)
I'm currently developing with Struts 1 a web application. I've recently started to use AJAX technology and I'm stuck with something.
I call an action method (via AJAX) to validate my form but no values are changed in the form bean when it gets to the action method. I suppose this is because calling the action via AJAX doesn't submit the form to the action method. Am I right? I've tried to send form values as a JSON object, but I can't parse it in the action class because; as far as I know, I need an external library to do so and, unfortunately, company policies doesn't allow me to use external libraries. Is there any other way to send the form?
Thanks in advance,
Carlos
Yes, you are correct. All that's happening with AJAX is a request is being sent to a particular URL, not a form submission.
The easiest way to deal with this is to add the fields you want validated as parameters in the URL then simply pull them off the HTTP request in the server (can't remember how easy that is with Struts).
I think this will do for you.
request = $.ajax({
url :'/your_action.do',
type :'post',
cache:false,
data :$("#formId").serialize()
});
the data field will send the required actionForm attributes.