Hello i am developing a web app...i am using MVC architecture in my design.
i have one form add_Flat.jsp in which admin enter the flat details and it will be stored in database (MS-Access) no other jsp page would be open on submit button.just a message displayed on add_Flat.jsp.
How can i achieve this by using MVC pattern ? do i have to write different servlet for every jsp page ? and model class for database connectivity ?
How can i achieve this by using MVC pattern ?
Let the form submit to a servlet which sets the message in the request scope and forwards back to the JSP.
do i have to write different servlet for every jsp page ?
Not necessarily. You can write a single servlet which acts as a front controller, or just use an existing MVC framework like JSF, Spring MVC, etc.
and model class for database connectivity ?
A model class should not be aware about any DB connectivity. A model class should be a simple Javabean class.
See also:
Our servlets wiki page - Contains Hello World examples and links to in-depth answers on servlet related questions here.
Related
as i understand MVC pattern as post MVC question
-
the Model is the part of the code that knows things
the View is the part of the code that shows the things the Model
knows
the Controller is the part of the code that gets commands from the
user and tells the View what to show and the Model what to know.
in spring-mvc we have DispatcherServlet which acts as controller gets command from client, also we have developer written controllers which serve commands and resolve the view and prepare Model for that view
in spring-mvc what exactly acts as controller DispatcherServlet or developer written controllers
Your #Controller annotated classes are your controllers. Think of the DispatcherServlet - as the name tells you - as dispatcher to your controllers.This is the same approach other MVC frameworks also choose (like Struts 2).
I'm migrating a Play! 1.2 web application and moving to Spring Boot + Spring MVC. Some views contain URLs to other endpoints. For example, I display the book title on the page and next to it I want to add the URL to go the book's details page (e.g. localhost/books/{id}).
In Play! 1.2 the controllers are static, and there is also a Router which can create the full URL for a method belonging to another controller (Router.getFullUrl("BookController.bookDetails", args)), but how do I achieve this with Spring MVC?
Best regards,
Cristian.
If you are trying to get the app/deployed name automatically in .jsp files to make the urls, then please make use of context path. An example below :
<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>
From your requirement "admin.myapp.com","admin-test.myapp.com" are server names right? Something like http://admin.myapp.com/book/{bookId},http://admin-test.myapp.com/book/{bookId}. In Spring app, relative path in jsp can be accessed using pageContext.request.contextPath
I also found the UriComponentsBuilder and ServletUriComponentsBuilder. They are similar to the Play! Framework router and provide methods for building URI's, handling parameters and the query etc. We chose to annotate the controllers' methods using constants and then use the same constants with the UriComponentsBuilder to build back the path and create the request query for GET requests.
Im new to jsp and MVC. Im studying this application that registers a customer into the database and then searches for it. The files that Im creating are Customer.java, Customer.jsp, CustomerDAO.java, CustomerContrtoller.java, CustomerService.java, result.jsp and ConnectionManager.java. How does this follow the MVC model? How do I know what is the model, view and controller in this app?
The MVC applies as follows to Jsps:
The model is your Customer.java. This model is used by the Jsp to render Html. The Jsp is the view. The CustomerController is the controller. The controller handles actions, like clicks, of the user, prepares the model and sends it to the Jsp.
Your confusion comes from the CustomerDao, I think. You have to understand that MVC is a frontend pattern. The model used in MVC can be the same as the Dao from the database, but most of the time it's not. Just the interaction between the Jsp, the controller and the model displayed on the Jsp is part of the MVC concept.
I have a quick question about how to use the Spring MVC.
I just started with it and i was used to have pages that always had their data loaded calling some controller that you setted ON THE PAGE.. calling some methods on controllers to load objects at the page at load time.
Is it the same in Spring MVC? Cuz what im seeing so far is that if you want to load a page with data, you always have to use a modelandview object which is loaded by a method that you must call before... and them you return the modelandview object with its destination to the view that you want.
What im trying to know is if there is a way that the page requests some data from the controller before it gets loaded... automatically...
Dont know if im making my self clear... thanks anyway for the help!
in spring mvc, using Controllers, it is always Request->Controller(populates model and chooses view)->Response.
you could use ajax to load data from any resource (even Controllers) or jsp:useBean to explicitly call some business logic or jsp:include to include a certain fragment or a custom jsp tag.
I also face the same situation.
check the below link for getting the data from the controller before view(JSP) load.
how to load the data into index page
I want to use Spring MVC 3.0 to build interfaces for AJAX transactions. I want the results to be returned as JSON, but I don't necessarily want the web pages to be built with JSP. I only want requests to the controllers to be intercepted/routed through the DispatcherServlet and the rest of the project to continue to function like a regular Java webapp without Spring integration.
My thought was to define the servlet-mapping url pattern in web.xml as being something like "/controller/*", then have the class level #RequestMapping in my controller to be something like #RequestMapping("/controller/colors"), and finally at the method level, have #RequestMapping(value = "/controller/colors/{name}", method = RequestMethod.GET).
Only problem is, I'm not sure if I need to keep adding "/controller" in all of the RequestMappings and no matter what combo I try, I keep getting 404 requested resource not available errors.
The ultimate goal here is for me to be able to type in a web browser "http://localhost:8080/myproject/controller/colors/red" and get back the RGB value as a JSON string.
You are not correct about needing to add the entire path everywhere, the paths are cumulative-
If you have a servlet mapping of /controller/* for the Spring's DispatcherServlet, then any call to /controller/* will be handled now by the DispatcherServlet, you just have to take care of rest of the path info in your #RequestMapping, so your controller can be
#Controller
#RequestMapping("/colors")
public class MyController{
#RequestMapping("/{name}
public String myMappedMethod(#PathVariable("name") String name, ..){
}
}
So now, this method will be handled by the call to /controller/colors/blue etc.
I don't necessarily want the web pages to be built with JSP
Spring MVC offers many view template integration options, from passthrough to raw html to rich templating engines like Velocity and Freemarker. Perhaps one of those options will fit what you're looking for.