0

我有一个简单的 JSP 项目,它有 3 个类、控制器和 DAO,我可以登录,但是当我单击按钮时,我无法从表单中获取参数。

让我展示一下代码,也许你会弄清楚:

package school.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import school.dao.StudentDAO;
import school.model.Student;

public class StudentController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static String UPDATE = "/student.jsp";
    private static String VIEW_COURSES = "CourseController";
    private static String VIEW_GRADES =  "GradeController";
    private StudentDAO dao;

    public StudentController() {
        dao = new StudentDAO();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String forward = "";
        String action = request.getParameter("name");

        if (action.equalsIgnoreCase("update")) {

            forward = UPDATE;
            Student student = (Student) request.getAttribute("student");
            request.setAttribute("student", student);

        } else if (action.equalsIgnoreCase("viewgrades")) {

            forward = VIEW_GRADES;

            Student student = (Student) request.getAttribute("student");
            request.setAttribute("student", student);

        } else if (action.equalsIgnoreCase("viewcourses")) {

            forward = VIEW_COURSES;
            Student student = (Student) request.getAttribute("student");
            request.setAttribute("student", student);
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }
}

这是 WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>School_JSP</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>CourseController</display-name>
    <servlet-name>CourseController</servlet-name>
    <servlet-class>school.controller.CourseController</servlet-class>
  </servlet>
  <servlet>
    <description></description>
    <display-name>StudentController</display-name>
    <servlet-name>StudentController</servlet-name>
    <servlet-class>school.controller.StudentController</servlet-class>
  </servlet>
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>school.controller.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
        <servlet-name>StudentController</servlet-name>
        <url-pattern>/StudentController</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CourseController</servlet-name>
        <url-pattern>/CourseController</url-pattern>
    </servlet-mapping>
</web-app>

和 mainPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Main Page</title>
</head>
<body>

    <h3>
        Welcome,
        <c:out value="${student.firstName}" />

        </br>
        <h1>My Personal Information</h1>
        <center>
            <form method="POST" action='StudentController' name="viewgrades">
                <input type="submit" value="View My Grades" />
            </form>
            <form method="POST" action='StudentController' name="viewcourses">
                <input type="submit" id="hidden" value="View My Courses" />
            </form>
            <form method="POST" action='StudentController' name="update">
                <input type="submit" id="hidden" value="Update Personal Information" />
            </form>
        </center>
    </h3>

</body>
</html>
4

2 回答 2

2

你混淆了属性和参数。

参数是浏览器发送的字符串值。getParameterXxx()使用方法族访问它们。

属性是您可以添加到请求(或会话或 servlet 上下文)或任何类型的数据,以便稍后获取它们。您通常在 servlet 中向请求添加一个属性(例如,有关登录用户的信息),以便在 JSP 中取回该属性并显示该信息。

在您的代码中,您获得参数的唯一位置被注释掉。而且您正在尝试获取一个名为“hidden”的参数,尽管没有一个表单具有名称以外的输入字段。它name是浏览器提交的输入字段的属性,而不是它的id属性。

此外,对于包含获取或读取内容的操作,您应该使用 GET 而不是 POST。

于 2013-04-27T22:21:18.213 回答
0

您没有得到任何值的原因是表单中的属性“名称”实际上只是表单的名称,根本没有发送。要实际发送数据,您需要在表单的 innerHTML 中提供一些。像这样(不是最好的解决方案,但你应该开始跑步):

<form method="GET" action="StudentController" name="formName">
  <input type="hidden" name="viewgrades" value="show" />
  <input type="submit" value="View My Grades" />
</form>

在此之后,您可以在 Java 类中读取发送的数据“viewgrades=show”(参数=值)。

另外,顺便说一句,您不应该将“h3”用作段落(“p”)或部分。它旨在成为标题。

于 2013-04-27T22:46:01.863 回答