【问题标题】:Getting an Error in JSP page as well as controller在 JSP 页面和控制器中出现错误
【发布时间】:2022-01-15 15:53:54
【问题描述】:

当我尝试运行我的 Spring 网页时,我的 JSP 页面出现错误。 错误是:

  1. 在第 [42] 行处理 [jsp/welcome.jsp] 时发生异常

  2. org.apache.jasper.JasperException: 在 [42] 行处理 [jsp/welcome.jsp] 时发生异常

  3. java.lang.IllegalStateException: Bean 名称“问题”的 BindingResult 和普通目标对象都不能用作请求属性

  4. 另一个错误是当我登录我的网络应用程序时,它仍然显示http://localhost:8080/caseStudyNew/login 而不是http://localhost:8080/caseStudyNew/welcomehttp://localhost:8080/caseStudyNew/login/welcome

下面是JSP页面和控制器的代码

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859 1" %>
<!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=ISO-8859-1">
    <title>Welcome</title>
    <style>
        * {
            font-family: cursive;
        }

        body {
            background-color: rgb(70, 187, 233);
        }
    </style>
</head>

<body>
    <tr>
        <td>Welcome ${firstname}</td>
    </tr>
    <br>
    <tr>
        <td><a href="home.jsp">Home</a></td>
    </tr>

    <h1 class="heading">Welcome</h1>
    <h2 class="heading">Problem shared is a problem solved!</h2>

    <form:form id="quesForm" modelAttribute="question" formactiom="question" method="post">

        <table>
            <tr>
                <td>
                    <form:label path="ques">Enter Your Question</form:label>
                </td>
                <td>
                    <form:input path="ques" name="ques" id="ques" />
                </td>
            </tr>

            <tr>
                <td>
                    <form:label path="ques_desc">Add the Description</form:label>
                </td>
                <td>
                    <form:input path="ques_desc" name="ques_desc" id="ques_desc" />
                </td>
            </tr>
            <tr>
                <td>
                    <form:button id="ques_submit" name="submit">Ask!</form:button>
                </td>
            </tr>

        </table>
    </form:form>

</html>

控制器代码:

package jbr.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import jbr.springmvc.model.Question;
import jbr.springmvc.service.UserService;


@Controller
public class QuestionController {

    @Autowired
    public UserService userService;

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public ModelAndView showRegister(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("welcome");
        mav.addObject("question", new Question());

        return mav;
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.POST)
    public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response,
                                @ModelAttribute("question") Question question) {

        userService.question(question);

        return new ModelAndView("Hey your", "question", question.getQues());
    }

}

型号代码

package jbr.springmvc.model;

public class Question {
    private int quesid;

    private String ques;
    private String ques_desc;


    public int getQuesid() {
        return quesid;
    }
    public void setQuesid(int quesid) {
        this.quesid = quesid;
    }

    public String getQues() {
        return ques;
    }
    public void setQues(String ques) {
        this.ques = ques;
    }
    public String getQues_desc() {
        return ques_desc;
    }
    public void setQues_desc(String ques_desc) {
        this.ques_desc = ques_desc;
    }
}

【问题讨论】:

    标签: java spring spring-mvc jsp


    【解决方案1】:

    尝试为 Question 类创建无参数构造函数。 公共问题(){ }

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2019-12-14
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多