I'm a bit confused about how MVC actually works.
So far this is the type of thing I have:
Static jsp page has a form that posts to the controller servlet.
Controller uses a database access object to grab the appropriate information from a database and stores it into a java bean.
Set that java bean as a request attribute, forward to some other jsp page for display.
What I'm confused about is what to do when the first jsp page I'm accessing isn't static and I need to retrieve a java bean for display without having some form to post to the controller to retrieve it.
It seems like if I call the database access object to generate the java bean I need from within my jsp page it'll be going against what MVC is.
So my question is how should this be handled? How should I retrieve the java bean I need in this case while sticking to MVC principles?
Thanks!
EDIT: I'll add some code to make it clearer:
At the moment I've got a page called owner.jsp which is supposed to display a list of hotels. Within that page I'm grabbing the list of hotels like this:
<%
HotelDAO hotelDAO = new HotelDAO();
List<HotelBean> hotels = hotelDAO.getHotels();
pageContext.setAttribute("hotels", hotels);
%>
Then using EL to display each hotel using a forEach loop.
This isn't going through a controller at all. How would I adapt this to work with the MVC model?
It doesn't seem logical to me to have do a check if the "hotels" bean is set and forward to a controller page that then sets the "hotels" bean and forwards back to the owner.jsp page.
How does MVC make sure the user always goes through the controller in cases like this? The other pages I've written previously were all static, and only when you submitted some form did it go to the controller, set some beans and forward to some page for display.
Related
I am using a session scope to store the bean,and i want to project the bean value to the jsp page when needed like this way
request.getSession().setAttribute("bean", bean);
response.sendRedirect("test.jsp");
And in the jsp i am using the below code to get the value on jsp
<% bean1 bean = (bean1) session.getAttribute("bean");
%>
<%= bean.getValue() %>
Instead of using a session scope i want to use a request scope,so can i set my attribute in my servlet in this way
request.setAttribute("bean", bean);
So how can i call it on my jsp
can i say
<% bean1 bean = (bean1) request.getAttribute("bean");
But it is showing error.Or instead of using scriplet how can i show my output using JSTL.
You're not understanding what a redirect is. A redirect is a response you send to the browser so that the browser sends another, new request to the location you redirected to. So, when you call sendRedirect("test.jsp"), the browser will send a new request to test.jsp. And obviously, all the attributes you have stored in the current request won't be available anymore.
It's impossible, without context, to say if a redirect is something you should do in this case, or if you should instead forward to the JSP. A forward is very different from a redirect, since it only transfers the responsibility of the current request and response to another component. In that case, there would be a unique request, and the JSP could find the attribute set by the servlet in the request.
The only thing I can say is that, in a properly designed MVC application, the JSP is used as a view, and there should never be a direct request to the view. Each request should go through a controller.
I want to pass parameters from one jsp to another.
I have tried using the post method, <jsp:forward/>, but it doesn't work.
I have created a <form> in html (parameters passed using POST), which is submitted to a servlet which processes the request and forwards it to another servlet that displays a page.
From this servlet i have created links to another jsp, passing through the parameters as GETs in the URL. However, I actually want to pass the parameters to another jsp using POST, and then pass it on to another jsp.
What solutions do you have or this problem?
Check out the Request Dispatcher. You need to forward the request to the landing JSP.
http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html
Sounds like you are creating a multipage form that gathers information from the user across several distinct pages. In that case one option is to use hidden fields on a form to store the previous values. This of course means that as the pages progress the amount of data passing back and forth from client to server increases.
You may consider a server side approach by storing the interim values in a database for instance, then only passing a token back to the client. When the next JSP page is submitted, use the token to look up the values in the database.
JSP has built-in request object.when one jsp redirect to another jsp with some parameter, you can get parameter value using this request object.
<%
String param1 = request.getParameter("parameter_name");
%>
you can find example here -
http://www.roseindia.net/jsp/RequestObjectInJSP.shtml
Why don't you call a page on the click of a submit button by creating
an url in the below format (in javascript):- var
url="your_page_name.jsp?value1="+encodeURIComponent(document.getElementById("your_text_field_or_any_other_field_id"));
and then call the page by using your url
document.your_form_name.action=url;
document.your_form_name.submit();
and then use request.getParameter() method either in servlet
or in the jsp that u'v metioned in the url (servlet or the jsp u'll be
calling thorugh u'r jsp).
I am working on a JSP project. I have a page that calls another JSP.
Now the problem is, how to pass or use a variable in the called JSP page in its calling page?
You can save the variable in the HTTPSession or ServletContext object.
And in the calling JSP page, use or check session attribute for the variable.
session.setAttribute(objectId, Object); to set the variable.
session.getAttribute(objectId); to get the variable.
I have a page that calls another jsp
page
The problem lies here in this sentence.
Try to follow MVC. Use JSP just for rendering the View, and Servlet as a Controller.
Here, simple.souther.us, you find simple and awesome tutorials for newbies.
Your design should be
take one input param on page1.jsp
post it to some servlet , process it there, forward request to page1 jsp and pass param taken from page 1 as attribute
See Also
why-business-logic-should-be-moved-out-of-jsp ?
I have a JSF page that loads a User and allows me to assign Roles to that particular User. The Backing Bean, AssignRolesBean is #RequestScoped, and I would like for it to remain so. However, here's my problem...
When the form submits, it calls AssignRolesBean.execute(). This then returns the path to the confirmation page. On this confirmation page, I want to show what new roles will be assigned and which role will be removed. However, I'm having trouble getting the User loaded on the confirmation page.
On the initial AssignRoles page, the userId is set using a GET parameter. It is then added as an h:inputHidden element on the page. It does get submitted. But, again, on the next page, the userId is not set (which loads the User). Is there any way I can keep the RequestScope and not have to store the userId in the SessionMap? I've been told that using hidden inputs will allow you to take data across pages. But, I'm having trouble with that.
if you develop jsf2 app, you can use flash scope to pass the parameters.
For more information about flash scope you can look at Learning JSF2: Using Flash scope
or you can put the parameters into session map
code snippet to get sessionMap:
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
Add the following to your view to execute an action upon a GET request:
<f:metadata>
<f:event type="preRenderView" listener="#{bean.init}" />
</f:metadata>
with
public void init() {
// Request parameters which are set by h:inputHidden, #ManagedProperty
// or f:viewParam are available here.
// You can just do e.g:
this.user = userService.find(userId);
}
I am using JSF 2.0 and attempting to pass values between different pages in my App.
The setup is as follows:
I have a page called userSelect that has a backing bean userSelectBacking. On this page I display a list of users that can be selected and submit using an h:commandbutton, when the page is submit the navigation goes to a userEdit page.
I have a page called userEdit, that has a backing bean userEditBacking which displays the information for a user
and allows that user to be edited.
I would like to pass the user selected from the userSelect page into the userEdit page.
I am currently using f:setPropertyActionListener to set the user in my userEdit backing from the userSelect page, however when I navigate to the userEdit page, it loses the information I set.
is there a way that I can pass the values between the two pages/backing beans?
thanks
I am currently using f:setPropertyActionListener to set the user in my userEdit backing from the userSelect page
It should work.
however when I navigate to the userEdit page, it loses the information I set.
This will happen if the data loading logic is wrong, or you fire a redirect afterwards while the bean is request scoped.
To fix the data loading logic, just ensure that in case of a request scoped bean the same datamodel is preserved in the subsequent request. Usually you use the bean's constructor or lazy loading in the getter for this. If that is not an option, then you need to put the bean in a bit broader scope, e.g. #ViewScope or #SessionScope.
To fix the redirect issue, either just don't fire a redirect (i.e. remove <redirect/> from navigation case, or don't call ExternalContext#redirect()), or put bean in a broader scope.