【问题标题】:@WebServlet don't work. getting null value in getAttribute in jsp@WebServlet 不起作用。在jsp的getAttribute中获取空值
【发布时间】:2015-07-21 22:21:47
【问题描述】:

我正在尝试将列表设置为请求属性以将其打印在 jsp 文件中。但是 showMentors.jsp 中的 getAttrubute 给出空值。 问题是控制器还是 web.xml? 请问有什么帮助吗? 我认为控制器无法正常工作。

Controller.java

@WebServlet("/Controller")
public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static SessionFactory factory = null;
    //set managers
    Mentors mentorsManager = Mentors.getInstance();

    public static SessionFactory getSessionFactroy() {
        try{
            if(factory == null) 
                factory = new AnnotationConfiguration().configure().buildSessionFactory();

        }
        catch(HibernateException e){
            System.err.println(e.getMessage());
        }
        finally{
            return factory;
        }
    }
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();//writer of the response
        String pathBuffer = request.getPathInfo();//the url

        switch(pathBuffer){     //switch for the url 
        case "/showMentors":
            List<Mentor> c = mentorsManager.getAllMentors();
            request.getSession().setAttribute("mymentors", c); // sending the coupons that the view will use

            request.getServletContext().getRequestDispatcher("showMentors.jsp").forward(request, response);
            break;

web.xml

  <?xml version="1.0" encoding="UTF-8"?>
<web-app 
  version="3.1" 
  metadata-complete="false"  
  xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">


    <display-name>WebPerachProject</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>com.sakhnin.implementations.Controller</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>index</servlet-name>
        <jsp-file>/jspFiles/index.jsp</jsp-file>
    </servlet>
    <servlet>
        <servlet-name>Mentor</servlet-name>
        <jsp-file>/jspFiles/Mentor.jsp</jsp-file>
    </servlet>

    <servlet>
        <servlet-name>showMentors</servlet-name>
        <jsp-file>/jspFiles/showMentors.jsp</jsp-file>
    </servlet>


    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/jspFiles/index.jsp</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>showMentors</servlet-name>
        <url-pattern>/jspFiles/showMentors.jsp</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Mentor</servlet-name>
        <url-pattern>/jspFiles/Mentors.jsp</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>/jspFiles/*</url-pattern>
    </servlet-mapping>

</web-app>

showMentors.jsp

  <%@page import="java.util.List"%>
<%@ page import="com.sakhnin.classes.*"%>
<%@ page import="com.sakhnin.implementations.*"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!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=windows-1255">
<title>All mentors</title>
<link type="text/css" rel="stylesheet" href="../styleFile/ourStyle.css" />
<link type="text/css" rel="stylesheet" href="../styleFile/bootstrap.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
</head>

<body>

<div data-role="header" data-theme="b">
        <h1>Show mentors page</h1>
        <div data-role="navbar">
            <ul>
                <li><a href="adminControlPanel" data-icon="home">all mentors </a></li>
            </ul>
        </div>
    </div>
<!-- Display a coupons page -->
            <div data-role="content" data-theme="b">
            <table class="table table-hover table-striped" id="mentors" height="100%" width="100%" border="3px" bordercolor="blue">
                <thead>
                    <tr style="color: green;">
                        <th>fullname</th>
                    </tr>
                </thead>
                <tbody>
                    aaaaaaaaaaaaaaa

                        hhhhhhhhhhhhhhhh
                        <%

                        // Retrieves the first page and display it

                        //List<Mentor>  m = (List<Mentor>)request.getAttribute("mymentors");
                    List<Mentor>  m = (List<Mentor>)request.getSession().getAttribute("mymentors");

                        System.out.println(m);
                        %>
                        <%
                        if (m!=null){


                            for(Mentor mentor : m) {
                    %>
                    <td>cccccccccccccccc<br>
                        <tr>
                            <td><%= mentor.getFullName()%><br>

                        </tr>
                    <% } }%>
                    xxxxxxxxxxxxxxxxxxxxx
                </tbody>
            </table>
            </div>
            <div data-role="footer" data-theme="b">
        <h4>BY: ASEEL & REMA</h4>
    </div>
</body>
</html>

【问题讨论】:

  • 如果我错了,请纠正我。在这里,在您的代码 (JSP) 中,我看不到请求的 /Controller 在哪里?
  • 我正在运行 showMentors.jsp,然后请求首先转到控制器,然后控制器将请求转发到 showMentors.jsp
  • 我发现了问题:控制器在我写的时候没有转发请求。我将案例“showMentors”更改为将请求转发到 index.jsp 而不是 showMentors.jsp,然后将其转发到 showMentors.jsp?请问有什么帮助吗?

标签: java jsp http servlets model-view-controller


【解决方案1】:

首先确保客户端首先调用/Controller,而不是showMentors.jsp

检查mentorsManager.getAllMentors();是否返回非空值,可能属性mymentors设置正确,但内容/值c为空;

我建议如果它是请求范围属性,您可以使用请求上下文而不是会话来传递参数/上下文。

【讨论】:

  • 如何确保客户端首先调用 /Controller ?我认为这是问题
  • 顺便说一下,我在 main.java 中检查了 MentorsManager.getAllMentors() 并且它工作正常。
  • 我发现了问题:控制器在我写的时候没有转发请求。我将案例“showMentors”更改为将请求转发到 index.jsp 而不是 showMentors.jsp 并将其转发到 showMentors.jsp?请问有什么帮助吗?
猜你喜欢
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 2020-05-28
  • 2018-07-21
  • 2012-03-31
  • 2018-09-20
  • 2015-12-31
  • 2018-10-17
相关资源
最近更新 更多