Announcement Announcement Module
Collapse
No announcement yet.
Simple MVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple MVC

    Hi there,
    Here is my Simple Spring MVC program [unfortunately not working ]

    == [web.xml] ==

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
    <servlet-name>HelloSpring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</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>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

    =========================================
    [HelloSpring-servlet.xml] :

    <?xml version = "1.0" encoding = "UTF-8" ?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
    <bean id = "urlMapping" class = "org.springframework.web.servlet.handler.SimpleUrl HandlerMapping">
    <property name = "mappings">
    <props>
    <prop key = "/hello.html">HelloSpringController</prop>
    </props>
    </property>
    </bean>

    <bean id = "HelloSpringController" class = "HelloSpringController" />

    </beans>
    ================================================
    [index.jsp, with valid refernce of jstl core tag]
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Simple Spring</title>
    </head>
    <body>

    <h1>JSP Page</h1>

    <form action = "hello.html" method = "post">
    Your Name?
    <input type = "text" name = "yourName" value = "" />
    </form>

    </body>
    </html>
    ================================================== =
    [welcome.jsp, with valid refernce of jstl core tag]
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>

    <h1>Welcome User</h1>

    Hello <c:out value = "${name}" />

    </body>
    </html>
    ===============================================
    [HelloSpringController.java]
    import javax.servlet.http.*;
    import java.util.*;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;

    public class HelloSpringController implements Controller
    {
    public HelloSpringController() {}

    public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception
    {
    String yname = req.getParameter("yourName");
    return new ModelAndView("welcome.jsp","name",yname);
    }
    }

    Actually the problem is that everything is running fine, but when I enter some name it is not going to displayed on "welcome.jsp". The MVC program is created using NetBeans5 IDE and source file is in the default package.

    Kindly help me. Thanks !

  • #2
    I think the problem is in your handleRequest-method:

    public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception
    {
    String yname = req.getParameter("yourName");
    return new ModelAndView("welcome.jsp","name",yname);
    }

    There isn't a constructor for ModelAndView with String, String, String -parameters.

    Try to put the yname String object into a Map and return the Map to the View as ModelAndView constructor argument ... something like this:

    Map model = new HashMap();
    String yname = req.getParameter("yourName");
    model.put("name", yname);
    return new ModelAndView("welcome.jsp", model);

    This is the way I'm transfering all the reference data to JSP-pages.

    Comment


    • #3
      web.xml

      Hi in your xml you used this element

      <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      I never succed with this element, instead I use the one that I found on every examples of the distribution. Use this and try it.

      <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
      <web-app>
      I can't tell you why is this way, because I don't know.

      Comment


      • #4
        you don't have a view resolver in your beans
        Code:
        	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        		<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
        	</bean>
        ps: your web-app declaration is absolutely fine, don't bother with 2.3 web-apps

        Comment


        • #5
          Simple MVC

          Hi,
          Thanks everybody out there.
          I have change the DTD, provides View resolver and also added the model as Map.

          But the problem remains as it is.

          Should I have to use commad class and command attribute to the XXX-servlet.xml.

          Comment


          • #6
            Hi AMP,

            The best advice I can give to you is follow the MVC step by step tutorial. It's very easy, and It works very well.

            Otherwise you can try this.

            When I use the standard.jar and jstl.jar that comes with the distribution
            I use this import

            <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
            and the 2.3 DTD

            <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
            <web-app>
            I just tried the 2.4 it works fine, first time I succeed. but I have to use jstl 1.1. In netbeans add librairy jstl 1.1. Also use this import
            <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
            I can't understand the link between the web app DTD and the jstl. But I tested and it seems to work like that. If someone understand this matter please tell or correct me.

            Hope this help
            Last edited by fanfaron; Oct 5th, 2006, 08:00 AM.

            Comment


            • #7
              Simple MVC

              Hi fanfaron,

              Its really working when I replaced my jstl and standard jar(I think these may belongs to obsolete version) with with NetBeans5 jar and also replace the DD with this:

              <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


              Thank Everybody and also for everything

              Comment

              Working...
              X