带有Spring Form Tag的JSP上的java.lang.ClassCastException

时间:2014-11-08 21:10:35

标签: java spring jsp spring-mvc jstl

我想加载一个包含Spring MVC中的表单标记的页面。我得到以下例外。我使用Spring 4.0.6.RELEASE(Context,Web,WebMVC,ORM)。 Web应用程序在嵌入式Tomcat 7.0.47中运行。

Nov 08, 2014 9:55:41 PM org.springframework.web.servlet.tags.form.FormTag doStartTag
SCHWERWIEGEND: org.springframework.web.context.support.XmlWebApplicationContext cannot be cast to org.springframework.web.context.WebApplicationContext
java.lang.ClassCastException: org.springframework.web.context.support.XmlWebApplicationContext cannot be cast to org.springframework.web.context.WebApplicationContext
    at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:83)
    at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:237)
    at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
    at org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
    at org.apache.jsp.WEB_002dINF.views.memberForm_jsp._jspx_meth_form_005fform_005f0(memberForm_jsp.java:124)
    at org.apache.jsp.WEB_002dINF.views.memberForm_jsp._jspService(memberForm_jsp.java:87)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Nov 08, 2014 9:55:41 PM org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[jsp] invoke
SCHWERWIEGEND: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: org.springframework.web.context.support.XmlWebApplicationContext cannot be cast to org.springframework.web.context.WebApplicationContext
    at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:83)
    at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:237)
    at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
    at org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
    at org.apache.jsp.WEB_002dINF.views.memberForm_jsp._jspx_meth_form_005fform_005f0(memberForm_jsp.java:124)
    at org.apache.jsp.WEB_002dINF.views.memberForm_jsp._jspService(memberForm_jsp.java:87)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Nov 08, 2014 9:55:41 PM org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[appServlet] invoke
SCHWERWIEGEND: Servlet.service() for servlet [appServlet] in context with path [/] threw exception [java.lang.ClassCastException: org.springframework.web.context.support.XmlWebApplicationContext cannot be cast to org.springframework.web.context.WebApplicationContext] with root cause
java.lang.ClassCastException: org.springframework.web.context.support.XmlWebApplicationContext cannot be cast to org.springframework.web.context.WebApplicationContext
    at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:83)
    at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:237)
    at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
    at org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
    at org.apache.jsp.WEB_002dINF.views.memberForm_jsp._jspx_meth_form_005fform_005f0(memberForm_jsp.java:124)
    at org.apache.jsp.WEB_002dINF.views.memberForm_jsp._jspService(memberForm_jsp.java:87)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

</web-app>

JSP页面

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Neues Mitglied</title>
    <link rel="stylesheet" type="text/css" href="/resources/style/style.css"/>
    <script type="text/javascript" src="/resources/js/jquery-1.11.1.min.js"></script>
</head>
<body>
<div id="toolbar">
    <a href="/la" class="backLink" title="Zurück zur Übersicht">Zurück zur Übersicht</a>
</div>
<div id="toolbarGap"></div>
<div id="formholder">
    <h1>${headline}</h1>
    <form:form action="/la/newmemeber" modelAttribute="trainingMember" method="post" id="memberForm">
        <div class="formRow">
            <form:hidden path="id"/>

            <div class="halfFormRow">
                <form:input path="prename" placeholder="Vorname"/>
            </div>
            <div class="halfFormRow">
                <form:input path="surname" placeholder="Nachname"/>
            </div>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <form:input path="birthday" placeholder="Geburtstag (z.B. 01.01.2000)"/>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <div class="halfFormRowTall">
                <form:input path="street" placeholder="Strasse"/>
            </div>
            <div class="halfFormRowSmallSecond">
                <form:input path="houseNumber" placeholder="Haus Nr."/>
            </div>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <div class="halfFormRowSmall">
                <form:input path="zip" placeholder="PLZ" size="5"/>
            </div>
            <div class="halfFormRowTallSecond">
                <form:input path="town" placeholder="Ort"/>
            </div>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <form:input path="email" placeholder="E-Mail"/>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <form:input path="telephone" placeholder="Telefon"/>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <form:input path="mobile" placeholder="Mobil"/>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <form:select path="trainingGroup">
                <c:choose>
                    <c:when test="${trainingGroup=='Gruppe 2'}">
                        <form:option value="Gruppe 1" label="Gruppe 1 (17:30 - 18:30)"/>
                        <form:option value="Gruppe 2" label="Gruppe 2 (18:30 - 19:30)" selected="selected"/>
                    </c:when>
                    <c:otherwise>
                        <form:option value="Gruppe 1" label="Gruppe 1 (17:30 - 18:30)" selected="selected"/>
                        <form:option value="Gruppe 2" label="Gruppe 2 (18:30 - 19:30)"/>
                    </c:otherwise>
                </c:choose>
            </form:select>
        </div>
        <p style="clear: both"></p>

        <div class="formRow">
            <input type="submit" class="inputButton" value="Speichern" name="submit" id="submit"/>
        </div>
        <p style="clear: both"></p>
    </form:form>
</div>
</body>
</html>

控制器

import de.ueberhammdesign.la.persistance.entities.TrainingMember;
import de.ueberhammdesign.la.web.services.TrainingMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;


@Controller
public class MemberEditingController {

    @Autowired
    private TrainingMemberService trainingMemberService;

    @RequestMapping(value = "/la/new", method = RequestMethod.GET)
    public String newMemberGet(Model model) {
        model.addAttribute("headline", "Neues Mitglied");
        model.addAttribute("trainingMember", new TrainingMember());
        return "memberForm";
    }

    @RequestMapping(value = "/la/newmemeber", method = RequestMethod.POST)
    public String newMemberPost(@ModelAttribute TrainingMember trainingMember, BindingResult bindingResult) {
        if(trainingMember.getId() == null) {
            trainingMemberService.createMember(trainingMember);
        } else {
            trainingMemberService.updateMember(trainingMember);
        }

        return "redirect:/la";
    }

    @RequestMapping(value = "/la/edit", method = RequestMethod.GET)
    public String updateMemberGet(HttpServletRequest request, Model model) {
        model.addAttribute("headline", "Neues Mitglied");
        model.addAttribute("trainingMember", trainingMemberService.getMeberByID(Integer.parseInt(request.getParameter("id"))));
        return "memberForm";
    }

    @RequestMapping(value = "/la/delete")
    public String deleteMember(HttpServletRequest request) {
        trainingMemberService.deleteMember((Integer.parseInt(request.getParameter("id"))));
        return "redirect:/la";
    }

}

0 个答案:

没有答案