jsp中(String)request.getAttribute("userid")获取为空
时间: 2024-06-03 11:12:53 浏览: 7
如果 `(String)request.getAttribute("userid")` 获取为空,有以下几种可能的原因:
1. 该属性在请求中并不存在。可能是在前一个页面中没有设置,或者在请求中被重置了。
2. 该属性在请求中存在,但是它的值为空。可以通过在设置属性时检查值是否为空来避免此情况。
3. 该属性在请求中存在,但是在获取属性值时使用了错误的属性名。请确保使用正确的属性名。
你可以在设置属性时使用 `request.setAttribute("userid", "xxxxx")` 来设置属性值。如果你已经确定该属性存在于请求中,可以尝试使用 `request.getParameter("userid")` 来获取属性值。此外,还可以在 JSP 页面中使用 EL 表达式 `${userid}` 来获取属性值。
相关问题
switch (method){ case "findAll": if(session.getAttribute("userName")!=null){ String userName= (String) session.getAttribute("userName"); String userId= userRepository.finduserId(userName); List<Contact> list = contactRepository.findAll(userId); req.setAttribute("list_contact",list); System.out.println(list); req.getRequestDispatcher("/index.jsp").forward(req,resp); // if (session.getAttribute("userName")!=null){ // req.getRequestDispatcher("/index.jsp").forward(req,resp); // } // else if(session.getAttribute("adminuserName")!=null){ // req.getRequestDispatcher("/adminAdress.jsp").forward(req,resp); // } } else if(session.getAttribute("adminuserName")!=null){ String userName= (String) session.getAttribute("userName"); List<Contact> list = contactRepository.findAll(); req.setAttribute("list_contact",list); System.out.println(list); req.getRequestDispatcher("/adminAdress.jsp").forward(req,resp); // if (session.getAttribute("userName")!=null){ // req.getRequestDispatcher("/index.jsp").forward(req,resp); // } // else if(session.getAttribute("adminuserName")!=null){ // req.getRequestDispatcher("/adminAdress.jsp").forward(req,resp); // } } break; case "deleteById": String id = req.getParameter("id"); contactRepository.deleteById(id); resp.sendRedirect("contact"); break;
这段代码使用了switch-case语句来处理不同的method参数值。
在"findAll"的情况下,代码首先检查session中是否存在"userName"属性。如果存在,则获取该属性的值作为userName,并通过userRepository.finduserId方法获取对应的userId。接着,调用contactRepository.findAll(userId)方法来获取该用户的所有联系人信息,并将结果存储在名为"list_contact"的request属性中。然后,通过req.getRequestDispatcher方法将请求转发到"/index.jsp"页面,以显示联系人列表。
如果session中不存在"userName"属性,而存在"adminuserName"属性,则进入else if分支。在该分支中,直接调用contactRepository.findAll方法获取所有联系人信息,并将结果存储在名为"list_contact"的request属性中。然后,通过req.getRequestDispatcher方法将请求转发到"/adminAdress.jsp"页面,以显示管理员的联系人列表。
在"deleteById"的情况下,代码通过req.getParameter方法获取id参数的值,并调用contactRepository.deleteById方法来删除指定id的联系人信息。然后,通过resp.sendRedirect方法将请求重定向到"contact"页面,以继续执行其他操作。
需要注意的是,这段代码只是一个片段,其中的其他代码(如userRepository和contactRepository对象的实现)并没有完整地展示出来。你需要根据具体情况进行适当的修改和完善。
6. 删除页面userdelete.jsp,获取main.jsp传递的用户ID参数,引JspUserDao Bean,调用其第2个方法,删除指定ID的用户信息,转回到main.jsp。
以下是一个示例的 `userdelete.jsp` 页面的代码,可以实现你的需求:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Delete User</title>
</head>
<body>
<h1>Delete User</h1>
<c:set var="userId" value="${param.userId}" />
<%
// 获取 JspUserDao Bean
JspUserDao dao = (JspUserDao) application.getAttribute("jspUserDao");
// 调用第2个方法,删除指定ID的用户信息
dao.deleteUser(Integer.parseInt((String)pageContext.getAttribute("userId")));
%>
<p>User deleted successfully.</p>
<a href="${pageContext.request.contextPath}/main.jsp">Back to Main Page</a>
</body>
</html>
```
在这个页面中,首先通过 `${param.userId}` 获取从 `main.jsp` 中传递过来的用户 ID 参数,并将它存储在 `userId` 中。然后获取 `JspUserDao` Bean,并调用其 `deleteUser()` 方法来删除指定 ID 的用户信息。最后显示一个成功消息,并提供一个链接回到 `main.jsp` 页面。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)