lost Coder
lost Coder

Reputation: 577

Creating a simple spring application with form

I am trying to create a form in the index.jsp file

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<title>INDEX</title>
<h2>Enter your query </h2>

<form:form method="post" action="addComplaint.html">
<table>
 <tbody><tr>
    <td><form:label path="query">Query</form:label></td>
    <td><form:input path="query"></form:input></td> 
</tr>

<tr>
    <td colspan="2">
        <input type="submit" value="Submit">
    </td>
</tr>
</tbody></table>

This is my 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

and my spring-servlet.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">



<context:component-scan base-package="test"/>
     <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp"/>

</bean>

I am fairly new to spring framework.The issue i am facing is whenever i add the spring forms in the jsp i am getting this error.

 HTTP Status 500 - java.lang.IllegalStateException: No WebApplicationContext   found: no ContextLoaderListener registered?

If I dont have the spring forms then there is no error.But when there is spring form i am getting this error. I have the controller class like this.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BotController {
@RequestMapping(value="/addComplaint",method=RequestMethod.POST)
public ModelAndView process()
{
    ModelAndView mav=new ModelAndView("index","key","value");
    return mav;
}

}

Upvotes: 0

Views: 513

Answers (4)

Otalk
Otalk

Reputation: 21

Add:

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

And the context file must be: [DispatcherServletName]-context.xml, Spring is configured to find that file first.

EDIT :

Spring implements the concepts of Inversion of Control, it's a container that manage the creation, the lifecycle and the dependencies of declared objects.

The declaration of these objects can be made via an application-context.xml or partially via spring annotations.

Spring documentation for the mvc concept

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

Upvotes: 2

Vignesh Shiv
Vignesh Shiv

Reputation: 1157

In your case, @RequestMapping should have .html extension.

servlet dispatcher url mapping you have like this,,

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Then RequestMapping should have .html extension.

 @RequestMapping(value="/addComplaint.html",method=RequestMethod.POST)
    public ModelAndView process() { }

Make sure that, Controller class package name in spring-servlet.xml,,

<context:component-scan base-package="test"/>

Upvotes: 3

Chetan Verma
Chetan Verma

Reputation: 873

Correct your JSP with matching close tag and add commandName attribute for form.

"Please make sure that the your index.jsp should be inside the WEB-INF/jsp folder"

Upvotes: 2

Akash Rajbanshi
Akash Rajbanshi

Reputation: 1593

Please add:

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

to your web.xml.

EDIT: Try replacing this:

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp"/>
</bean>

To:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
</bean>

Upvotes: 1

Related Questions