我尝试将列表设置为请求属性以在jsp文件中打印它。但showMentors.jsp中的getAttrubute给出null值。 问题是控制器还是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>
答案 0 :(得分:0)
首先确保客户端先调用/Controller
,而不是showMentors.jsp
检查mentorsManager.getAllMentors();
是否返回非空值,可能是属性mymentors
设置正确,但内容/值c
为空;
我建议如果它是一个请求范围属性,您可以使用请求上下文而不是会话传递参数/上下文。