SPRING处理JSP页面时发生异常

时间:2016-06-13 11:18:34

标签: java spring maven tags


我对Spring的标签有疑问 当我运行jsp页面时,它给了我这个错误跟踪:

 HTTP Status 500 - An exception occurred processing JSP page /newExamTypology.jsp at line 19
type Exception report
message An exception occurred processing JSP page /newExamTypology.jsp at line 19
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 /newExamTypology.jsp at line 19
16:     <form:form modelAttribute="examTypology" >  
17:         <div>
18:             <br />
19:             <form:input path="name"></form:input>
20:             <br /><br />
21:             <form:label path="code">Codice:</form:label> <br />
22:             <form:input path="code"/>
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:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'examTypology' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.newExamTypology_jsp._jspx_meth_form_005finput_005f0(newExamTypology_jsp.java:193)
    org.apache.jsp.newExamTypology_jsp._jspx_meth_form_005fform_005f0(newExamTypology_jsp.java:129)
    org.apache.jsp.newExamTypology_jsp._jspService(newExamTypology_jsp.java:86)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.

我无法理解为什么,事实上我使用正确的uri作为标签 在这里,有我的cose页面!

newExamTypology.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://www.springframework.org/tags/form"
    prefix="springForm"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Pagina creazione nuova tipologia d'esame</title>
</head>
<body>
    <h1>Creazione nuova tipologia d'esame</h1>
    <div>Inserimento dati:</div>
<%--    <form:form method="POST" commandName="examTypology" action="updateexamtypology"> --%>
    <form:form modelAttribute="examTypology" >  
        <div>
            <br />
            <form:input path="name"></form:input>
            <br /><br />
            <form:label path="code">Codice:</form:label> <br />
            <form:input path="code"/>
            <br /><br />
            <form:label path="description">Descrizione: </form:label><br />
            <form:input path="description"/>
            <br /><br />
            <form:label path="cost">Costo: </form:label><br />
            <form:input path="cost"/>
            <input type="submit" value="invia" />
        </div>
    </form:form>

    <div>Naviga nel sito:</div>
    <div>
<!--        <a href="newExamTypology.jsp">crea nuova tipologia d'esame</a> -->
        <p>Ciao!</p>
    </div>
</body>
</html>

ExamTypologyController.Java

package it.uniroma3.controller;

import java.util.List;

import javax.validation.Valid;

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.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
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;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import it.uniroma3.model.ExamTypology;
import it.uniroma3.service.ExamTypologyService;

@Controller
@RequestMapping("/examTypology")
public class ExamTypologyController extends WebMvcConfigurerAdapter{

    @Autowired
    private ExamTypologyService examTypologyService;

    @Autowired
    @Qualifier("examTypologyValidator")
    private Validator validator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.setValidator(validator);
    }

    @RequestMapping(value = "/examTypology", method = RequestMethod.POST)
    public ModelAndView examTypology() {
        return new ModelAndView("examTypology", "command", new ExamTypology());
    }

//  @ModelAttribute("examTypology")
//  public ExamTypology createExamTypologyModel() {
//      return new ExamTypology();
//  }

    @RequestMapping(value="/listexamtypology", method = RequestMethod.GET)
    public String listExamTypology(Model model) {
        model.addAttribute("examTypologiesList", examTypologyService.listExamTypology());
        return "examTypology";
    }   

    @RequestMapping(value = "/addexamtypology", method = RequestMethod.POST)
    public String addExamTypology(Model model) {
        return "newexamtypology";
    }

    @RequestMapping(value="/updateexamtypology", method = RequestMethod.GET)
    public String updateExamTypology(@ModelAttribute("examTypology") @Valid ExamTypology examTypology, BindingResult bindingResult, Model model) {
        if(bindingResult.hasErrors()) 
            return "addexamtypology";
        this.examTypologyService.insertExamTypology(examTypology);
        model.addAttribute("examTypologiesList", examTypologyService.listExamTypology());
        return "examTypology";
    }

    @RequestMapping(value="/delete/{etId}", method = RequestMethod.GET)
    public String deleteExamTypology(@PathVariable("etid") Long etId, Model model) {
        this.examTypologyService.deleteExamTypology(etId);
        model.addAttribute("examTypologiesList", examTypologyService.listExamTypology());
        return "examTypology";
    }


}

先谢谢!

1 个答案:

答案 0 :(得分:1)

您似乎没有将模型本身传递给该页面。基本上你需要做以下这样的事情:

@RequestMapping(value = "/newExamTypology", method = RequestMethod.GET)
public String showNewExamTypology(ModelMap modelMap) {
    // your custom logic
    modelMap.put("examTypology", new ExamTypology());
    return "newExamTypology";
}