0

I am creating a registration form in spring tool suite.After creating project it run fine. It show welcome message on run. But whenever i creating the home page with jsp and spring tag library it gives following error

HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 23

HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 23

type Exception report

message An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 23

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 23

20:   
21:    <table width="400px" height="150px">
22:     <tr>
23:      <td><form:label path="firstName">First Name</form:label>
24:      </td>
25:      <td><form:input path="firstName" />
26:      </td>


Stacktrace:
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:405)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'firstName' available as request attribute
 org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
 org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
 org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
 org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
 org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
 org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
 org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
 org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
 org.apache.jsp.WEB_002dINF.views.home_jsp._jspx_meth_form_005flabel_005f0(home_jsp.java:188)
 org.apache.jsp.WEB_002dINF.views.home_jsp._jspService(home_jsp.java:99)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:405)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.9 logs.
Apache Tomcat/8.0.9

when i remove the fields from table it run fine showing the hrading etc .But when creating fields it give error

Home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Being Java Guys | Registration Form</title>
</head>
<body>
 <center>

  <div style="color: teal; font-size: 30px">Being Java Guys |
   Registration Form</div>



  <c:url var="userRegistration" value="saveUser.html" />
  
   <table width="400px" height="150px">
    <tr>
     <td><form:label path="firstName">First Name</form:label>
     </td>
     <td><form:input path="firstName" />
     </td>
    </tr>
    <tr>
     <td><form:label path="lastName">Last Name</form:label>
     </td>
     <td><form:input path="lastName" />
     </td>
    </tr>
    
     <td><input type="submit" value="Register" /></td>
    </tr>
   </table>
  
  <a href="list">Click Here to see User List</a>
 </center>
</body>
</html>

Controller class

package com.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
 
 private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
 
 
 @RequestMapping(method = RequestMethod.GET, value = "/")
 public String home(){
  return "home";
 }
 
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.spring</groupId>
 <artifactId>controller</artifactId>
 <name>SpringHibernate</name>
 <packaging>war</packaging>
 <version>1.0.0-BUILD-SNAPSHOT</version>
 <properties>
  <java-version>1.6</java-version>
  <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>
 <dependencies>
  <!-- Spring -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${org.springframework-version}</version>
   <exclusions>
    <!-- Exclude Commons Logging in favor of SLF4j -->
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
     </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>
    
  <!-- AspectJ -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency> 
  
  <!-- Logging -->
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>${org.slf4j-version}</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>${org.slf4j-version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>${org.slf4j-version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.15</version>
   <exclusions>
    <exclusion>
     <groupId>javax.mail</groupId>
     <artifactId>mail</artifactId>
    </exclusion>
    <exclusion>
     <groupId>javax.jms</groupId>
     <artifactId>jms</artifactId>
    </exclusion>
    <exclusion>
     <groupId>com.sun.jdmk</groupId>
     <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
     <groupId>com.sun.jmx</groupId>
     <artifactId>jmxri</artifactId>
    </exclusion>
   </exclusions>
   <scope>runtime</scope>
  </dependency>

  <!-- @Inject -->
  <dependency>
   <groupId>javax.inject</groupId>
   <artifactId>javax.inject</artifactId>
   <version>1</version>
  </dependency>
    
  <!-- Servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.1</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
 
  <!-- Test -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.7</version>
   <scope>test</scope>
  </dependency>        
 </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Servlet-context.xml

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

 <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
 
 <!-- Enables the Spring MVC @Controller programming model -->
 <annotation-driven />

 <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
 <resources mapping="/resources/**" location="/resources/" />

 <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
 <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <beans:property name="prefix" value="/WEB-INF/views/" />
  <beans:property name="suffix" value=".jsp" />
 </beans:bean>
 
 <context:component-scan base-package="com.spring.controller" />
 
 
 
</beans:beans>

1 Answers1

1

Your configuration is right, however, you have an issue in your controller such that, the controller responsible for returning the view should return a ModelAndView instance. Upon receiving a request, any BaseCommandController will attempt to fill the command object using the request parameters. This is done using the typical and well-known JavaBeans property notation. When a request parameter named 'firstName' exists, the framework will attempt to call setFirstName([value]) passing the value of the parameter. Nested properties are of course supported. For instance a parameter named 'address.city' will result in a getAddress().setCity([value]) call on the command class. The form-tags default model attribute command. You should replace your controller with the code below:

    @RequestMapping(method = RequestMethod.GET, value = "/")
public ModelAndView personForm(Locale locale, Model model) {
    return new ModelAndView("home","command", new User());
}

"home" is the view you are directing to "command" is a default model attribute "new User()" is the instance which contains setters and getters for its properties. In the view that contain the form you have to make sure that the path attributes's value should have setters and getters in the User class.<form:input path="firstName" />, for example firstName should be equal to the firstName attribute in the User class moreover it should have setters and getters.

Reference: https://docs.spring.io/spring-framework/docs/1.2.9/javadoc-api/org/springframework/web/servlet/mvc/BaseCommandController.html

Ahmed Awji
  • 11
  • 3