| Author |
MVC without using struts
|
K Anshul
Greenhorn
Joined: Oct 20, 2004
Posts: 4
|
|
I am trying to use MVC in my JSP application. For each page I have 3 pages, one for view , one for model and one for controller. Say i have an html form (form1.html) which the user fills and submits. This form goes to the controller (servlet1.java) which calls a java class, Model (model1.java) to perform database operations and business logic and the request is dispathed to the view (jsp1.jsp). Similarly, If user enters a query to search in form2.html, it goes to servlet2.java which calls model2.java and then dispatches request to jsp2.jsp to display the result to the user. In this way if I have say 10 forms, then i'll have 10 jsp's , 10 servlets and 10 models, names of each model and view hard coded into the servlet. I know this is not the right way to do it. So many controllers and hard coded names of servlets and jsps make it look weird. There must be a better way of doing it. In chapter 14 of HF Servlets & JSP it is mentioned that Struts can be used to solve this problem. But at the moment i can't learn Struts. Is there any way i can use just Servlets and JSP to solve this problem. From page 55 HF, something like this I am doing in my app I want to solve this prob without using Struts.
|
 |
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
Since your pages are not going to be reused by different flows, you may simply specify the target Servlet that your form wanna to be submitted to. And that Servlet can determine which page will be forwarded to after the process. This can then by pass the usage of Struts. But still, seems using Struts gives you more flexibility, isnt it? Nick
|
SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
|
 |
K Anshul
Greenhorn
Joined: Oct 20, 2004
Posts: 4
|
|
Originally posted by Nicholas Cheung: Since your pages are not going to be reused by different flows, you may simply specify the target Servlet that your form wanna to be submitted to. And that Servlet can determine which page will be forwarded to after the process. This can then by pass the usage of Struts. But still, seems using Struts gives you more flexibility, isnt it? Nick
Does that mean having 1 controller for each model and view is correct ? And will hard coding the name of view and model in the controller be correct ?
|
 |
Mary Wallace
Ranch Hand
Joined: Aug 25, 2003
Posts: 138
|
|
Anushal, You dont have to 10 servlets for that. For each page you have pass a hidden value and action can be one servlet. if the page name is "abc" do this in that servlet, that way one servlet will do for 10 servlets.
|
 |
Mary Wallace
Ranch Hand
Joined: Aug 25, 2003
Posts: 138
|
|
|
If the number of pages are less there is no need to go with structs.
|
 |
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
Does that mean having 1 controller for each model and view is correct ? And will hard coding the name of view and model in the controller be correct ?
You dont need to. You can specify all the names of possible JSPs in the configuration file's <init-param>, where the param name is view name, while value is the target model name. And as Mary said, for each JSP, you can submit the view name via hidden field, so that you check which model you are going to use (or create by reflection). Nick
|
 |
Bryan Basham
author
Ranch Hand
Joined: Apr 30, 2001
Posts: 199
|
|
Hi all, There are several trade-offs when deciding to use only one servlet controller or multiple servlet controllers. Here is a rough list: one servlet for the whole webappone servlet for each use case in the webappone servlet for each action in each use case in the webapp I have found that if you take the first and second approaches that your servlet(s) will be very complicated with lots of conditional or dispatching code (based on the hidden "action" field as suggested by Mary). The alternative (third approach) is to use a different servlet for each action in your webapp. This design implies a proliferation of servlet classes that need to be built for your webapp. This approach has several positive aspects. First, each servlet is highly cohesive (which is good OO). Second, it is easy to add new actions to your webapp. Finally, if you do decide to refactor your webapp to use Struts, this approach makes it much easier as each servlet simply becomes a subclass of the Action class. HTH, Bryan
|
 |
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
That's why Struts implements MVC in that way. If you dont use Struts, you may need to implement MVC, better refers to what Sturts did. Nick
|
 |
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
|
|
That's why Struts implements MVC in that way. If you dont use Struts, you may need to implement MVC, better refers to what Sturts did.
Was Sturts the father of Struts?
|
SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
|
 |
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
Sorry for the typos. Nick
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: MVC without using struts
|
|
|