I primarily use PHP when making websites so I might reference them from time to time.
First lets make a new project: File → New Project

Then go to: Java Web → Web Application → Next

Next in project name call the application MusicChecker making sure “Use Dedicated Folder for Storing Libraries” is selected then click next

now where going to use the glassfish server and Java EE 6 web, making sure “Enable Contexts and Dependency Injection” is not selected we just dont need it at the moment but research it as you might like to know then click next.

We now need to select Spring Web MVC and in the library select a framework, I chose 3.2.3 but 2.5 works fine too.
In configuration you get to name the dispatcher, in the dispatcher you basically declare all the beans. (the objects used in the website) you also have a dispatcher mapping option, this is what the website would be seen as by the browser i.e. index.jsp would change to index.htm and anything looking for a htm file would look for a jsp instead.
Name the dispatcher MusicChecker and after doing this click finish. Netbeans will make everything in the background that would be needed to make the page work.
Understanding the environment
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
The index controller
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
lets break things down for you;
If you open a page i.e. the index page the view resolver would find its corresponding jsp file and by looking at the url mapping for the request i.e. index.htm would be index.jsp in the jsp folder as shown in the line p:prefix="/WEB-INF/jsp/" and p:suffix=".jsp" /> this shows where the jsp files are and what to look for i.e. jsp files.
<prop key="index.htm">indexController</prop>
this line states: Ok I found the JSP file so how does it work, i.e. what is the name of the controller in this case it is indexController, so what is that, its a link to where the java class is in this case its a built in class class="org.springframework.web.servlet.mvc.ParameterizableViewController"
you can use any java class you want as long as it extends the Controller class and returns a model and view.
Lets run this and see what it looks like.

As you can see it brings you strait to a website, this is running on your glass fish server but I’m guessing your thinking the same thing I did. How in the hell did this page get created I didn't write any of this crap.

remember naming the dispatcher MusicChecker netbeans would add the suffix -servlet to that so your dispatcher would be named
remember naming the dispatcher MusicChecker-servlet.xml thats the code shown above.
In the project tab select MusickChecker → Web Pages → jsp
You should now see index.jsp this is the page that we just saw in the last example, open it and you would see its just html, nothing special just HTML you can change that to say what ever you want then run again if you have time to play.
Now lets move to why it opened the index page first, as you know my default the browser would look for the index page by default but spring isn't like that, as you can see in the picture above theirs a file called redirect.jsp when you open the website for the first time it would look in that fist and that would redirect the page to index.htm the mapper would then change that to index.jsp open that file. But thats not needed in this project so lets delete redirect.jsp after doing this the page will not load. As explained browsers automatically look for index.htm when a root is opened so I bet your asking why hasn't it opened.
That’s easy Spring MVC uses something called a welcome file, this sorts out what file to open first the redirect.jsp file could be used to test what browser is being used i.e. mobile desktop etc. and open a page that supports that browser. But lets not go into that, if you open the web.xml document seen above you will see this:
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
this tells spring what to open first as we have removed the redirect.jsp file you can remove those three lines. Now the browser would look for index.htm.
Run the code and you will see the index page.
Making pages
Now lets make our own page.
Right click the jsp folder and select new → JSP

now name it Upload and click finish, simple enough right now if you go to your browser and change index.htm to Upload.htm you should see your page right, wrong although you have the jsp file the system hasn't a clue to how to access it although we have a mapper looking for htm requests spring wouldn't automatically distinguish it.
Remember these lines:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
This tells spring what to look for and how to use it. Inside <props>...</props> where going to add the line:
<prop key="Upload.htm">UploadController</prop>
this just shows where to look when the browser asks for the upload page. If you run this it wont work that’s because theirs no such thing as the UploadController. So lets make it:
In the projects tab right click source packages → new → java class
Name the class UploadController and name the package Controller

add implements Controller to the class name and when the light bulb error comes up click it and select import org.springframework.web.servlet.mvc.Controller;

this would now make your class a controller, in spring MVC the controller would need to make a model for the view this is called a model and view. So another light bulb error will pop up, click it again and select implement all abstract methods, this just adds this code.
@Override
public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
This automatically creates a default model and view in this case a not supported error message will be shown.
So we have a controller now lets run the page although it wouldn't work actually it wouldn't even run why you ask because UploadController might exist but where and how do you use it. Remember this code in the dispatcher:
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
This tells spring how to use the index page, we need one of these for UploadController.
<bean name="UploadController"
class="Controller.UploadController"
/>
I removed the viewName because its not needed and as you can see in the class I structured it package.controller name
now remember this:
<prop key="Upload.htm">UploadController</prop>
notice UploadController this is the same as the bean name so this line connects the Upload.htm page to the UploadController class.
So now when you go to that address it will work like this: opens the controller run the modelandview function opens the view i.e. Upload.jsp using the model made in the Upload Controller if you add everything then run the page you need to go to the Upload.htm page.

This is what you will get, the model makes this error. That’s because we don't make a model we just make a error message:
look at the UploadController.java file:
@Override
public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
lets delete throw new UnsupportedOperationException("Not supported yet.");
As you can see the code returns a ModelAndView so lets make one add the code:
ModelAndView mv = new ModelAndView(“Upload”);
the ModelAndView constructor don’t need an argument but I like to give it a name for the sake of it but it has to be the same name as the jsp file now add
return mv;
then save and open the upload page again.

Ok so now it works but so what all it does is opens “JSP” files that are basically just html documents, right?
Wrong jsp files can use Java in them all you need to do is use the syntax <% %> anyone that uses php would know this its the same as <? ?> just write what ever Java you want in it and it would work. We wont be using this in this tutorial but try it if you want it is useful.
Now lets have a look at the model, before the return statement add
mv.addObject("example", "this is an example");
this adds the string “this is an example” to the model and names it example, now open the Upload.jsp file. Delete hello world and replace it with ${example} this will get the string we just created and print it where it stands.

Now we pretty much know everything we need to make spring our MVC web pages, so lets start.
No comments:
Post a Comment