Another basic Spring tutorial

After hours of figuring out how this Spring thingy works..finally came up with one simple app using controllers and stuff. So lets create an app to display values entered by user.

Jar files usually needed for Spring are :


  • spring. jar
  • standard.jar
  • jstl.jar
  • taglibs-string. jar
  • commons-lang.jar
  • commons-logging-1.1.jar
  • spring-webmvc-2.5.jar
  • log4j-1.2.14.jar
You can get the jar files you need here.

Create a new project, here I've used the name springtest. So far no jar files are needed except the one which comes together with the netbeans project file. While creating new project, change the *.htm to *.do coz we want to use it the .do way :P (refer below)





Create a jsp file under WEB-INF/jsp. Name it as test.jsp. (or any other name u like :P) Inside this test.jsp add the following code :

<html>
<head>
<meta equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<form method="POST" action="/springtest/dispatcher/saveform.do">
First Name:
<input name="firstName" type="text" value="">
Last Name:
<input name="lastName" type="text" value="">
<input type="submit" value="Save Changes">
</form>
</html>

springtest here is the project name. dispatcher here is the "dispatcher"-servlet.xml. if you give the name as name-servlet.xml then your action will look like this :: /springtest/name/saveform.do.


Now we're goin to edit dispatcher-servlet.xml.

change this "<prop key="/index.htm">indexController</prop>" to this "<prop key="/test.do">indexController</prop>" coz we're goin to connect it to test.jsp. well practically u can give it any name. Its just like the name of ur .do in struts :P So your code should look like this.




<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.do">indexController</prop>
</props>
</property>
</bean>


and that code is related to this one.. see the word "index" above? its the same index in the code below. How it works and all..will be figured out soon :P

<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" /
>

So if you want to load any file, just replace the word index with the file name. In this case, change them to test. so it becomes test.do. In redirect.jsp, change the url to "test.do". Try running the project and see if it runs correctly
. For some reason its not possible to run the specific file. It only works by running the whole project. This will be figured out soon as well..lol so patience!

So the final code :

<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>


<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/test.do">indexController</prop>
</props>
</property>
</bean>

<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="test" /
>

Create a success page. Name it as testsuccess.jsp for instance. Under this file, add the following code:

<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
//dont forget to add this at the top!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Name <c:out value="${name}"/></h2>

</body>
</html
>


c:out here is somewhat the same as our bean write in struts. Here the name is the attribute name that we have passed from controller. We will see that below.

Create a package under source packages. Name it as controller.

Under controller package, create java class called FormController.java. Inside this class, add the following codes :

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class FormController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("test");
//RequestDispatcher that will send the request to /WEB-INF/jsp/test.jsp
}
}

Create second file under the controller package called SaveController.java. Add the following code:

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class SaveController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
return new ModelAndView("testsuccess", "name" , firstName + lastName);
//RequestDispatcher that will send the request to /WEB-INF/jsp/testsuccess.jsp
}
}

In this code, testsuccess is the jsp file. name is the attribute name which will be accessed by getParameter(), and firstName+lastName is the name that is sent thru that name attribute.

Then lastly add these last 2 lines inside the dispatcher-servlet.xml.

<bean name="/dispatcher/displayform.do" class="controller.FormController"/>
<bean name="/dispatcher/saveform.do" class="controller.SaveController"/
>

these lines are similar to our struts-config.xml. bean name is the action name in struts. class is your java class (package.javaclassname)

Try running the whole application again. The output should be something like below :









This app is done without using form bean. That will be another tutorial :P

Posted By - Lakshmi

2 comments:

Anonymous said...

public class EmpFormController and EmpSaveController, discard the "Emp"...right?
already try to build n run ...got error..
HTTP Status 404
description: The requested resource() is not available

Kooky Kitten said...

Thnx..din notice :P