1

CASE1: Iam getting below error :

 org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp at line 14

11: <body>
12:     
13:     
14:      <form:form  method="get" commandName="user" action="login">
15:      
16:         <form:label path="Username" /><form:input path="uname"/>
17:         <form:label path="Password" /><form:input path="password"/>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
    org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:85)
    org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:209)
    org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
    org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
    org.apache.jsp.login_jsp._jspx_meth_form_005fform_005f0(login_jsp.java:111)
    org.apache.jsp.login_jsp._jspService(login_jsp.java:76)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

WHEN IAM USING BELOW FORM:

<form:form  method="post" commandName="user" action="login">

        <form:label path="Username" /><form:input path="uname"/>
        <form:label path="Password" /><form:input path="password"/>

        <input type="submit" value="Submit" />
     </form:form>

CASE2: Iam getting 404 after when I SUBMITTED below form, I mean this url : http://localhost:8443/BugTrackingSystem/login

WHEN IAM USING BELOW FORM:

<form action="login" method="post">
        Username: <input type="text" name="uname">
        Password: <input type="text" name="password">
        <input type="submit" value="Submit">
    </form>

OTHER FILES: *LOGINCONTROLLER:*

package com.bts.controller;


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

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.bts.vo.User;

public class LoginController extends SimpleFormController{


     public LoginController(){
            setCommandClass(User.class);
            setCommandName("user");
        }



     protected ModelAndView onSubmit(Object obj) throws ServletException {

         User user = (User) obj;
            System.out.println("username: "+user.getUname());


        return new ModelAndView("index","user", user);

     }


}

WEB.XML

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>BugTrackingSystem</display-name>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>



</web-app>

USER.JAVA

package com.bts.vo;

import java.util.ArrayList;
import java.util.List;

public class User {

    public User() {

        countries = new ArrayList<String>();

        countries.add("India");
        countries.add("US");

    }

    private String uname;
    private String password;
    private List<String> countries;

    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public List<String> getCountries() {
        return countries;
    }
    public void setCountries(List<String> countries) {
        this.countries = countries;
    }

}

DISPATCHER-SERVLET.XML

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>     


</beans>

CONTROLLERS-BEANS.XML

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">



    <bean name="login" class="com.bts.controller.LoginController">
        <property name="successView" value="index" />
    </bean>

    <bean name="productDetails"
        class="com.bts.controller.ProductDetailsController">
    </bean>



</beans>

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <import resource="dispatcher-servlet.xml"/>
  <import resource="controllers-beans.xml"/>
  <import resource="datasource.xml"/>
  <import resource="hibernate-beans.xml"/>


</beans>

I am learning right now Spring, so I'm facing above problems, so please provide me a solution, I will use Annotations later after when I understood this issue..

Irwin
  • 105
  • 1
  • 2
  • 7
  • remove the ´/´ in the bean names. – Angelo Fuchs May 27 '13 at 08:16
  • HI, I tried as you said, BUT still iam getting same errors, BTW I have updated above my stacktrace... pls chk it, if possible please run from your end.. – Irwin May 27 '13 at 08:19
  • Yeah, its not the only problem, I look into it. – Angelo Fuchs May 27 '13 at 08:20
  • Have you seen this post? http://stackoverflow.com/questions/11014782/adding-context-loader-listener-to-web-xml-in-spring-mvc – Angelo Fuchs May 27 '13 at 08:22
  • you're saying me to remove '/' from this tag, right??? , HERE: I did as you said, BUT still iam getting same errors as above....I tried using BOTH forms.. iam not understanding what still changes need to do, can u find any other errors in my xml files? – Irwin May 27 '13 at 08:32
  • I seen your link, BUT i have "contextConfigLocation" inside , instead of using , so am I doing wrong here? – Irwin May 27 '13 at 08:34
  • You've edited your question to where it no longer reflects reality. You claim to receive an error that your code won't generate. If your original question is answered, you should accept the answer and ask another question if you have one. – Ryan Stewart May 27 '13 at 16:52
  • too much problem, you better create different questions for differents problems. – storm_buster May 27 '13 at 17:40

1 Answers1

0

I recommend the following links:

http://duckranger.com/2012/04/spring-mvc-dispatcherservlet/

http://viralpatel.net/blogs/spring-3-mvc-handling-forms/

http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

And this setup worked for me:

web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    [...]
</web-app>

application-context.xml

<beans xmlns="http://www.springframework.org/schema/beans">
    <import resource="myOtherContext.xml"/>
</beans>

myOtherContext.xml (for the sake of having some import)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean name="login" class="test.LoginController"/>
</beans>

dispatcher-context.xml (Note that this is auto-addressed because my dispatcher servlet is called dispatcher and it appends -context.xml automatically)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

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

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

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

</beans>

login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>
    <body>
        <form:form  method="post" commandName="user" action="login.htm">
        <form:label path="uname" /><form:input path="uname"/>
        <form:label path="password" /><form:input path="password"/>
        <input type="submit" value="Submit" />
     </form:form>
    </body>
</html>

LoginController.java

public class LoginController extends SimpleFormController {

    public LoginController() {
        setCommandClass(User.class);
        setCommandName("user");
    }

    @Override
    protected org.springframework.web.servlet.ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {

        return super.showForm(request, response, errors, controlModel);
    }

    @Override
    protected ModelAndView onSubmit(Object obj) throws ServletException {

        User user = (User) obj;
        System.out.println("username: " + user.getUname());
        return new ModelAndView("index", "user", user);
    }
}
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • so, cant we use an application without using application-context.xml file? is it a MUST file?? BTW, i run with your above code and will let you know now.. – Irwin May 27 '13 at 08:38
  • BTW, should I neeed to use this? org.springframework.web.context.ContextLoaderListener in this link given: http://stackoverflow.com/questions/7704286/java-lang-illegalstateexception-no-webapplicationcontext-found-no-contextloade – Irwin May 27 '13 at 08:39
  • No, its not. You can use the import statements in every context.xml. It is convention to name the top level context.xml `application-context.xml` and work down from there. – Angelo Fuchs May 27 '13 at 08:40
  • @Irwin Please have a read of http://duckranger.com/2012/04/spring-mvc-dispatcherservlet/ to get to know what the DispatcherServlet does and how (etc.) I edited into my answer as well. – Angelo Fuchs May 27 '13 at 08:50
  • sure, i will definitely read that link, now iam having a doubt, should I use imports now, since iam using ContextLoaderListener, if u dont mind, can u just show me the code of yours application-context.xml and , web.xml file (full UPDATED code) – Irwin May 27 '13 at 08:57
  • @Irwin I have not run your code, yet. I might if the suggestions I gave you don't yield the result. When I do, I'll give you the codes. But when you learn a new technique its important to work yourself into it. – Angelo Fuchs May 27 '13 at 09:05
  • atleast can u tell me , should I need to use import tags now, since iam using a Listeneer, right? – Irwin May 27 '13 at 09:07
  • Yes. Use `` and `import` – Angelo Fuchs May 27 '13 at 09:16
  • I just updated web.xml, application-context.xml, controllers-beans.xml files in my 1st post, now when I try to run using , iam getting this exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute – Irwin May 27 '13 at 09:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30676/discussion-between-angelo-neuschitzer-and-irwin) – Angelo Fuchs May 27 '13 at 09:34
  • when iam using with normal form tag, iam getting same 404 error for the url: http://localhost:8443/BugTrackingSystem/login (this url comes after when I submitted my form) – Irwin May 27 '13 at 09:35
  • I tried with your updated code, BUT iam still getting same exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute – Irwin May 27 '13 at 14:53
  • @Irwin Then, I'm sorry, I can't help you any further. This code of mine is a working example. It actually runs over here. So whatever you do, it must be something different then what I did. Please create a new Example Project and add exactly the code as I provided it and test it again. – Angelo Fuchs May 27 '13 at 16:50
  • HI, iam using now Annotations, iam getting same exception even using annotations also, can u please check my code here: http://stackoverflow.com/questions/16777156/java-lang-illegalstateexception-neither-bindingresult-nor-plain-target-object-f/16789190?noredirect=1#16789190 – Irwin May 28 '13 at 14:01