Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HTTP Status 500 – Internal Server Error
- Type Exception Report
- Message An exception occurred processing JSP page [/person.jsp] at line [39]
- Description The server encountered an unexpected condition that prevented it from fulfilling the request.
- Exception
- org.apache.jasper.JasperException: An exception occurred processing JSP page [/person.jsp] at line [39]
- 36: </c:if>
- 37: <tr>
- 38: <td>
- 39: <form:label path="name">
- 40: <spring:message text="Name"/>
- 41: </form:label>
- 42: </td>
- Stacktrace:
- org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
- org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
- org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
- org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
- javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
- Root Cause
- java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'person' available as request attribute
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
- <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
- <%@ page session="false" %>
- <html>
- <head>
- <title>Person Page</title>
- <style type="text/css">
- .tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;}
- .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;}
- .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}
- .tg .tg-4eph{background-color:#f9f9f9}
- </style>
- </head>
- <body>
- <h1>
- Add a Person
- </h1>
- <c:url var="addAction" value="/person/add" >
- <form:form action="${addAction}" commandName="person">
- <table>
- <c:if test="${!empty person.name}">
- <tr>
- <td>
- <form:label path="id">
- <spring:message text="ID"/>
- </form:label>
- </td>
- <td>
- <form:input path="id" readonly="true" size="8" disabled="true" />
- <form:hidden path="id" />
- </td>
- </tr>
- </c:if>
- <tr>
- <td>
- <form:label path="name">
- <spring:message text="Name"/>
- </form:label>
- </td>
- <td>
- <form:input path="name" />
- </td>
- </tr>
- <tr>
- <td>
- <form:label path="country">
- <spring:message text="Country"/>
- </form:label>
- </td>
- <td>
- <form:input path="country" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <c:if test="${!empty person.name}">
- <input type="submit"
- value="<spring:message text="Edit Person"/>" />
- </c:if>
- <c:if test="${empty person.name}">
- <input type="submit"
- value="<spring:message text="Add Person"/>" />
- </c:if>
- </td>
- </tr>
- </table>
- </form:form>
- </c:url>
- <br>
- <h3>Persons List</h3>
- <c:if test="${!empty listPersons}">
- <table class="tg">
- <tr>
- <th width="80">Person ID</th>
- <th width="120">Person Name</th>
- <th width="120">Person Country</th>
- <th width="60">Edit</th>
- <th width="60">Delete</th>
- </tr>
- <c:forEach items="${listPersons}" var="person">
- <tr>
- <td>${person.id}</td>
- <td>${person.name}</td>
- <td>${person.country}</td>
- <td><a href="<c:url value='/edit/${person.id}' />" >Edit</a></td>
- <td><a href="<c:url value='/remove/${person.id}' />" >Delete</a></td>
- </tr>
- </c:forEach>
- </table>
- </c:if>
- </body>
- </html>
- package com.ma3stro.crudperson.controller;
- import com.ma3stro.crudperson.model.Person;
- import com.ma3stro.crudperson.service.PersonService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- @Controller
- public class PersonController {
- private PersonService service;
- @Autowired(required=true)
- public void setService(PersonService service) {
- this.service = service;
- }
- @RequestMapping(value = "/persons", method = RequestMethod.GET)
- public String listPersons(Model model) {
- model.addAttribute("person", new Person());
- model.addAttribute("listPersons", service.listPersons());
- return "person";
- }
- //add and update
- @RequestMapping(value = "/person/add", method = RequestMethod.POST)
- public String addPerson(@ModelAttribute("person") Person p) {
- if(p.getId() == 0)
- service.addPerson(p);
- else
- service.updatePerson(p);
- return "redirect:/persons";
- }
- @RequestMapping(value = "/remove/{id}")
- public String removePerson(@PathVariable("id") int id) {
- service.removePerson(id);
- return "redirect:/persons";
- }
- @RequestMapping("/edit/{id}")
- public String editPerson(@PathVariable("id") int id, Model model) {
- model.addAttribute("person", service.getPersonById(id));
- model.addAttribute("listPersons", service.listPersons());
- return "person";
- }
- }
- package com.ma3stro.crudperson.service;
- import com.ma3stro.crudperson.model.Person;
- import com.ma3stro.crudperson.repository.PersonRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Service;
- import javax.transaction.Transactional;
- import java.util.List;
- @Service
- public class PersonServiceImpl implements PersonService {
- private PersonRepository repository;
- // Здесь нужен бин из AppContext или Autowired?
- @Autowired
- @Qualifier("personDAO")
- public void setRepository(PersonRepository repository) {
- this.repository = repository;
- }
- @Override
- @Transactional
- public void addPerson(Person p) {
- repository.addPerson(p);
- }
- @Override
- @Transactional
- public void updatePerson(Person p) {
- repository.updatePerson(p);
- }
- @Override
- @Transactional
- public void removePerson(int id) {
- repository.removePerson(id);
- }
- @Override
- @Transactional
- public List<Person> listPersons() {
- return repository.listPersons();
- }
- @Override
- @Transactional
- public Person getPersonById(int id) {
- return repository.getPersonById(id);
- }
- }
- package com.ma3stro.crudperson.repository;
- import com.ma3stro.crudperson.model.Person;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.stereotype.Repository;
- import java.util.List;
- @Repository
- public class PersonRepositoryImpl implements PersonRepository {
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- @Override
- public void addPerson(Person p) {
- Session session = sessionFactory.getCurrentSession();
- session.persist(p);
- }
- @Override
- public void updatePerson(Person p) {
- Session session = sessionFactory.getCurrentSession();
- session.update(p);
- }
- @Override
- public void removePerson(int id) {
- Session session = sessionFactory.getCurrentSession();
- Person p = (Person) session.load(Person.class, new Integer(id));
- if(p != null)
- session.delete(p);
- }
- @SuppressWarnings("unchecked")
- @Override
- public List<Person> listPersons() {
- Session session = sessionFactory.getCurrentSession();
- return session.createQuery("from person").list();
- }
- @Override
- public Person getPersonById(int id) {
- Session session = sessionFactory.getCurrentSession();
- Person p = (Person) session.load(Person.class, new Integer(id));
- return p;
- }
- }
- package com.ma3stro.crudperson.model;
- import javax.persistence.*;
- @Entity
- @Table(name = "person")
- public class Person {
- @Id
- @Column(name = "id")
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- @Column(name = "name")
- private String name;
- @Column(name = "country")
- private String country;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- @Override
- public String toString() {
- return "Person{" +
- "id=" + id +
- ", name='" + name + ''' +
- ", country='" + country + ''' +
- '}';
- }
- }
- <?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" xmlns:tx="http://www.springframework.org/schema/tx"
- 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
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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="/" />
- <beans:property name="suffix" value=".jsp" />
- </beans:bean>
- <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <beans:property name="url"
- value="jdbc:mysql://localhost:3306/person" />
- <beans:property name="username" value="root" />
- <beans:property name="password" value="root" />
- </beans:bean>
- <!-- Hibernate 4 SessionFactory Bean definition -->
- <beans:bean id="hibernate4AnnotatedSessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <beans:property name="dataSource" ref="dataSource" />
- <beans:property name="annotatedClasses">
- <beans:list>
- <beans:value>com.ma3stro.crudperson.model.Person</beans:value>
- </beans:list>
- </beans:property>
- <beans:property name="hibernateProperties">
- <beans:props>
- <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
- </beans:prop>
- <beans:prop key="hibernate.show_sql">true</beans:prop>
- </beans:props>
- </beans:property>
- </beans:bean>
- <beans:bean id="personDAO" class="com.ma3stro.crudperson.repository.PersonRepositoryImpl">
- <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
- </beans:bean>
- <!--<beans:bean id="personService" class="com.ma3stro.crudperson.service.PersonServiceImpl">-->
- <!--<beans:property name="personDAO" ref="personDAO"></beans:property>-->
- <!--</beans:bean>-->
- <context:component-scan base-package="com.ma3stro.crudperson.controller" />
- <context:component-scan base-package="com.ma3stro.crudperson.repository" />
- <context:component-scan base-package="com.ma3stro.crudperson.service" />
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
- </beans:bean>
- </beans:beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement