Get command object - java

I am writing a spring 2.5 application and in my jsp I'm writing my own tags.
It's about a list of objects...when I change the number of rows that list shows(a combobox), I am doing a submit on my form returning back to the view(obviosly with the new number of rows returned).
When listing with my own tags I need to get the properties from my command object.
I have access to the pageContext object but I can't figure where the command object is stored.

By default, the command object is stored under a "command" attribute (request or session scope depending on your configuration of the sessionForm property). You can change that by setting the commandName property on your controller and your command object will be included in the model under this name (and not the default "command").
Once in your tag code, you can use request.getAttribute("command") or, if sessionForm=true, session.getAttribute("command") to get access to your command object (assuming the default name "command"). If you changed the name of the command using the commandName property then use that instead of "command".
Usually you wouldn't care in what scope the command is, so having access to the pageContext object, you can do a pageContext.findAttribute("command") and that will look it up in all scopes.

Related

Using a value as an argument into another value in Thymeleaf

So lets say I have a #ModelAttribute of userCredentials which is a List<String> object.
and I have another ModelAttribute of type Map<String,String> of roles.
I can access them separately in HTML using Thymeleaf with:
${userCredentials.contains('<Hardcoded-value>')}
The problem i want the hardcoded value replaced and to use for example:
${userCredentials.contains('roles.client')}
Do you know how can i successfully use a model attribute as a parameter to the other model attribute. It works with the hardcoded values
You can use Thymeleaf pre-processing:
${userCredentials.contains(__${roles.get('client')}__)}
Thymeleaf executes the preprocessing part in a first pass. So it will replace what is there with:
${userCredentials.contains(<result of roles.get() call here>)}
And then execute the template rendering.

JavaBeans: Can not read Variables from JavaBeans class

i got a simple programm that begins on an input form where the user fills in 3 numbers. The form action refers to a controller servlet where i store the data in the Bean class with the setter methods I have defined.
number.setNumber1(Double.parseDouble(request.getParameter("number1")));
Till now I stored the Number object in the request with
request.setAttribute("numbers", number);
and forwarded it to the output page where i could get it with ${numbers.biggestNumber ( getter-Method that simply determines the biggest Number) }. A tutorial i am using says I could also get the data directly from the Bean by using this piece of code:
<jsp:useBean id="num" scope="session" class="model.Numbers"/>
<c:out value="${num.biggestNumber}/>
but somehow the Bean uses another object of the Numbers-class. I see the advantage of this technique, because I dont have to put the Numbers object into the request. Can someone tell me how I can use the same Numbers object I stored the data before?
I already read that I shouldnt use "jsp:setProperty..." to store the data on the input page, but if i cant get the information i wrote manually to the Bean, I have to ask myself why I should use the JSP JavaBeans annotation at all.
I used the search function but could not find an answer suitable to my question, or maybe I am just not experienced enough to get them in a more advanced context... Any help would be welcome
if you are using this one,
request.setAttribute("numbers", number);
And using requestDispacher redirecting then at target page you can do likewise,
into JSP file :
<jsp:useBean id="numbers" scope="request" class="model.Numbers"/>
<c:out value="${numbers.biggestNumber}/>
Here,
you did with wrong scope=session, means you are putting value into 'request' scope and try to pull it from session is wrong.
also maintain name of attribute 'same' while putting/getting from scope. here, name="numbers" maintain while putting/getting
Okay my mistake was that I thought JavaBeans-Jsp-Tags would save time and code. Indeed I had to create a HttpSession-Object that stores the ID of the used JavaBean
HttpSession sess = request.getSession(true);
sess.setAttribute("number", number);
Now the JavaBean-Tag in my Output.jsp knows which object to use (the one created in the Input.jsp). As far as I do understand now, the only advantage of the JavaBean-Jsp-Tag above normal Parameters added to the Request is that I can use the Bean-Class in the whole Session and am not dependend on the Request-Object.

How do I validate a field in a jelly form that depends on the form state?

I'm building a Jenkins plugin that accesses a remote server. As part of the configuration, the user needs to enter a URL for the server and provide an access key. Then the plugin will retrieve a list of resources from the server, and the user needs to select the appropriate one from an html select input.
I'm using the /lib/form namespace to create the config.jelly file, and the select is being populated using the tag:
<f:entry title="Resource" field="resource">
<f:select />
</f:entry>
In my Descriptor class, I have a method:
doFillResourceItems(#QueryParamter final String url, #QueryParameter final String key)
and the stapler appears to call the method any time the onchange event is triggered by the browser (as expected).
My problem is in the doCheckResource(#QueryParameter final String value) method. It would be helpful to have access to the ListBoxModel object that was returned by the doFillResourceItems() method, as the error message I want to display to the user should be different, depending on whether the plugin was able to retrieve the list of resources or not. One option would be to simply inject the url and key fields into the doCheckResource method as well, and try again, but them I'm retrieving the list of resources twice, which isn't ideal.
What do I need to do in order to have Stapler inject that previously retrieved ListBoxModel object into the validation method call?

Struts2 file upload errors

I am a using struts2 file upload and my action class contains 3 private fileds with getter and setters
private File myFile;
private String myFileFileName;
private String myFileContentType;
I have some douts to clarify
We are passing only the file as parameter and bind it to the myFile, So how the application getting the file name and content type?
whenever I use myFileVariableName + "FileName" (if the file variable is myFile then file name variable is myFileFileName, if file is xxx, then file name is xxxFileName), I am getting the output, if i make any change to this format (ie,myFileVariableName + "FileName"), It getting null. Is it mandatory to use this format? Can I change it to any name I desire? If so, then how?
To get the content type, I should use jst "contentType" or myfileVariableName + "contentType". Is it also mandatory?
I assume, if I use a separate bean to store my request variables, all the parameters is bind to that bean variable. But in the case of file upload only the file variable ie, myFile in this example only get and set in the bean. fileFileName and contentType are null. If I declare these variables directly in my action class, then I get the values, but whenever I use a separate bean, only File variable can get and set, and the other two are null. Why?
If I use ModelDriven, the the same case happening, I can only get File variable and the other two variables are null. why?
I am only extending the "struts-default" in my struts.xml and no separate config for file upload, since it dont show any effect in my questions.
Action class for the file upload, declare a File variable to store the user uploaded file, two String variables to store the file name and content type. The fileUpload interceptor will auto inject the uploaded file detail via set 'X' ContentType() and set 'X' FileName(), make sure the method name is spell correctly.
The file upload function is depends on the “fileUpload Interceptor“,
make sure it is included in the Action’s stack. The lucky is, the
default stack is already includes the “fileUpload Interceptor“.
The fields userImageContentType and userImageFileName are optional. If setter method of these fields are provided, struts2 will set the data. This is just to get some extra information of uploaded file. Also follow the naming standard if you providing the content type and file name string. The name should be ContentType and FileName.
For example if the file attribute in action file is private File
uploadedFile, the content type will be uploadedFileContentType and
file name uploadedFileFileName.
Get Set Behaviour in Struts 2 : Assign value to a variable, not property value.
For example,
public class SetTagAction extends ActionSupport{
private String msg;
public String setMsg(String msg) {
this.msg = msg;
}
<s:set var="msg" value="%{'this is a message'}" />
Many Struts 2 developers thought that the set tag var=”msg” will assign the value to the associated action class via setMsg() method.
This is wrong, the set tag will not call the setMsg() method, it will only assign the “value” to a variable named “msg“, not the action’s property value.

How to Change init-parameters at Runtime?

If I modify the XML to change the value of init parameter
I see the changes only when web-app is redeployed.
My question is cant I get around this by setting the values at run time.Is there any API that allow me to change the values dynamically.
It's called init-parameter for a reason. So, you can't.
But you can change values at runtime, that's no problem.
After reading the init parameters put them as attributes of the ServletContext (ctx.setAttribute("name", value))
Create a small (password-protected) page that lists all attributes of the ServletContext and gives the ability to change them.
Maybe you could use apache commons configuration, specifically have a look at Automatic Reloading...
Make use of properties files instead and write code so that it 1) reads the value from it everytime, or 2) can reload the value on command, or 3) reloads the file automatically at certain intervals.
If you put the properties file somewhere in the webapp's runtime classpath or add its path to the webapp's runtime classpath, then you can easily access/load it as follows:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
String value = properties.get("key");

Categories