Form Navigation in Spring - spring

Hi I have four JSP file say one.jsp, tow.jsp, three.jsp and four.jsp
each file has next and previous link to navigate to the all jsp's and last jsp file that is four.jsp has submit button. Now I want values of all fields from first three jsp's into the fourth one, to submit data into DB when the use hit the submit button from four.jsp
I do not have to use the hidden fields to pass the values from jsp to jsp and I don't want to use the session to store the data. Is there any way to pass the data(VO) from one jsp to another without using hidden fields and session?
NOTE : I am using spring. and I am not interested in JSF for navigation.

So you need a stateful bean, might want to look into something like Spring Web Flow. It was made for handling this sort of stuff.
If you don't want to store it in the session, you would have to start looking into EJB which has stateful possibilities.
Here's some documentation:
http://static.springsource.org/spring/docs/2.5.6/reference/ejb.html
P.S. EJB stores stuff in the session as well, don't think there's any getting around it, unless you serialize the POJO and store it as a cookie.

Related

I want to change the home page menu depending on the login user. How do i do it in Spring MVC?

I am able to log-in using Spring-MVC.
Now i would like to change the menu's depending upon the user who logged in.
How do i pass login-username to my home page using spring MVC Controller?
So that i could use that UserName and change the menu according to Login-name?
Is there any framework available for same?
What if i want to send a collection of objects (Which will contain the actions assigned to the login user ) to my home page after successful login?
Is there any better way to do it?
any suggestions would be more appreciated. Need a Help.
If you want to handle this based on roles then spring security provides jsp taglib to handle this.
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html
Example
<sec:authorize access="hasRole('supervisor')">
This content will only be visible to users who have
the "supervisor" authority in their list of <tt>GrantedAuthority</tt>s.
</sec:authorize>
In addition to Bhushan's answer, you can also use your custom UserDetails class to store information pertaining to the authenticated user, and pass values to the model from your controller class with model.put("myParamName", myParam);
Then, in the JSP, with or without the authorize tag, you can refer to model attributes with ${myParam}. Note that this is not limited to pure HTML content; you may pass Javascript in this fashion as well -- just be wary of the fact that while what is passed may be whatever data type you like (that JSP can handle, anyway), it will be treated as a String when ultimately rendered, so you'll have to escape characters and include line breaks and/or tabs for lengthy content and proper formatting. For example:
MyController.java
. . .
String myJavascript = "<script>\n\tvar myVar = \"Hello Dheeraj!\";\n\talert(myVar);\n</script";
model.put("someJavascript", myJavascript);
. . .
myPage.jsp
${someJavascript}
Now, when that page is called, the client will see the following:
. . .
<script>
var myVar = "Hello Dheeraj!";
alert(myVar);
</script>
You can of course also pass arrays, etc., and loop through items to build <select> elements, etc. The user-specific logic will presumably reside on (or be called from) your controller class.

Getting partial view fragments with Spring MVC

I'm new in Spring MVC, I just started my first project and I'm doing some research to be sure to set it up in a proper way (should work in the long-term!).
I already know that for a part of the project, I will need to manually change small fragments of the page through Ajax. I know it's possible to change part of the page (using Tiles). What I really need, though, is for example to change a single line in a table containing dynamically generated data (i.e. data coming from the database).
Can you suggest anything?
I don't want to use JSF or Spring JS.
Thank you.
You have at least two choices:
render on the server, send the update html snippet to the brower and use JavaScript to replace them
send an AJAX request to the server, but this time return only the data (JSON) and the "render" the table line in the browser (or just update some pices of text)
For the fist choice you need a dedicated jsp file (and tiles configuration) to render only a single line. As fare as I know, there is no technical support.
What you can do, to reduce the amount of duplicated code is to use that single line rendering jsp in like in include in the one that renders the complete table.
Of course instead of using JSP to render the single line you can also use the Java Method that handles the request, and make it returning the html string.

use ajax to fetch data from java hashtable in jsp page

I'm working on a project that the web page will fetch the data from java hashtable object in jsp page. I'm using a jsp for gerenated the web page based on HTML. The data is stored in the java hasbtable object on the server. I want to try to make an AJAX call to fetch the data from the server, then display it in the jsp page.
I just want to know if this is possible to do that by make an AJAX call to access the java hashtable object, then fetch the data back to the client side.
Thanks
Here is the test.jsp page which contain the hashtable obejcts:
Hashtable generalTable = (Hashtable) metaDataTable.get("General");
Hashtable adminTable = (Hashtable) metaDataTable.get("Administration");
My inital approach is to make an AJAX call to this test.jsp page. Then try to access those two GeneralTable and adminTable hashtable objects. In those two obejcts, it contains the values I would like to fetch.
Unfortunately, I don't have the code yet for my part because I don't know if this is possible or not.
Yes, it's possible, but you will need to have some server-side code to deal wit the request for the data you want, just like with any other AJAX functionality in any other system.
One way to do this would be to have a "special" jsp that gives you back the data you need without any of the typical HTML. Having the special jsp output it's data as a JSON object will make your life much easier in the client-side code.

Spring MVC AbstractWizardFormController Question

I am using a controller implementation that is extending the Spring MVC
AbstractWizardFormController
This wizard controller will consist of 4 pages. The first 2 pages are used to collect information. The third page will show results based on what information is submitted on page 1 and 2.
So to be a little more specific
Page 1 the user will select a state and some other information
Page 2 the user will enter more information such as contact information
Page 3 will display information dependent on the information collected in first two pages
There is more pages after this, but they do not pertain, so if the first think you are thinking of is using onSubmit(), then it wont work because it is not the end of the controller life.
I need to collect all the data from the first two pages, and then run a db query and return it to the third page. where and how is the best way to do this, do I run the query in reference data when returning to the third page?
You can use postProcessPage method. Its API is clear
Post-process the given page after binding and validation, potentially updating its command object. The passed-in request might contain special parameters sent by the page.

Codeigniter - reusing controllers?

I am trying to code my first codeigniter project. I have a login controller which basically filters the data inputed and calls a model function that checks if the user is found in the database.
What I am trying to do is reuse this controller on the index page. So basically I want to be able to do user login on the index page or on the normal controller page (index.php/login/) without code duplication.
I'm sure there is an easy way to do this, but I'm not sure what the best solution is. Make it a library?
Thanks!
For this I would simply make the form in your view post to the login controller.
As a more generic way to share code and logic throughout your application, take a look at this article:
CodeIgniter Base Classes: Keeping it DRY
You basically give each of your controllers a "type". Being logged in could be a criteria of one of your base controllers, which saves you trying to directly access any of your controllers which is bad mojo.
You can try creating a form on the index page and submit it to index.php/login/. This way you won't need two entry points.
Just do the same as you have done for the login View, specify the same action attribute of the form to the index View, and it will be sent to the same login controller with no need to create the two login controllers. You might want to append a query string in the action attribute of the form to distinguish from which View the request has come.

Resources