Saturday, February 12, 2011

Hello Spring + Netbeans

Core Java + Spring + Netbeans
Download the jar files from spring-framework-2.5.4.zip
Add to lib.
spring-framework-2.5.4\dist folder and modules folder and spring.jar and spring-sources.jar files
HelloSpring:


HelloSpring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/hello.html">HelloSpringController
                </prop>
            </props>
        </property>
     </bean>  
        <bean id="HelloSpringController"
          class="HelloSpringController"/>
</beans>

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
   <servlet>
        <servlet-name>HelloSpring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
        </servlet>
    <servlet-mapping>
        <servlet-name>HelloSpring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
            <welcome-file>
            first.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>


First.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello Spring</title>
    </head>
    <body>
        <form action="hello1.html" method="POST">
            What is your name?  <input type="text" name="yourName" value="harshal" />
            <input type="submit" />
        </form>
    </body>
</html>

Welcome.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

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

HelloSpringController.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
       
public class HelloSpringController implements Controller{
       
    public HelloSpringController() {
    }
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
       String yourName= request.getParameter("yourName");
        ModelAndView modelAndView = new ModelAndView("welcome.jsp");
        modelAndView.addObject("yourName", yourName);
        return modelAndView;
    }
}

No comments:

Post a Comment