01/10/2018, 11:08
Hỏi về lỗi Cannot find bean: "studentList" in any scope
Chào các bạn. Mình có sử dụng strust để làm upload csv lưu vào database sau đó lấy lên JSP. Tuy nhiên sau đó mình gặp một lỗi khi lấy lên jsp là nó ko tìm đc bean scope. Các bạn giúp mình với
Config xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="fileUploadForm" type="form.FileUploadForm"></form-bean>
</form-beans>
<action-mappings>
<action input="/input.jsp" path="/upload" name="fileUploadForm"
attribute="fileUploadForm" type="action.FileUploadAction" scope="request">
<forward name="success" path="/pages/studentList.jsp" />
</action>
<action input="/studentList.jsp" path="/listStudentPage"
type="action.StudentListPageAction" name="studentList">
<forward name="success" path="/pages/studentList.jsp" />
</action>
<action path="/listStudentPage" parameter="/pages/studentList.jsp" type="action.StudentListPageAction" />
</action-mappings>
<message-resources parameter="action.ApplicationResources" />
</struts-config>
File UploadForm:
package form;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
public class FileUploadForm extends ActionForm{
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
// @Override
// public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
// ActionErrors errors = new ActionErrors();
// if (file.getFileSize() == 0) {
// errors.add("file", new ActionMessage("error.file.required"));
// } else if (!file.getContentType().equals("application/csv")) {
// errors.add("file", new ActionMessage("error.file.type"));
// }
// //logs debug
// if(logger.isDebugEnabled()){
// logger.debug("WelcomeAction.execute()");
// }
//
// //logs exception
// logger.error("This is Error message", new Exception("Testing"));
//
// return mapping.findForward("success");
// /**
// * If the file size is greater than 20kb.
// */
// else if (file.getFileSize() > 20480) {
// errors.add("file", new ActionMessage("error.file.size"));
// }
// return errors;
//
//}}
File StudentList:
package action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import bean.Student;
import dao.StudentDAO;
import pagination.Pages;
public class StudentListPageAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
StudentDAO studentDAO = new StudentDAO();
List<Student> studentList = new ArrayList<>();
//get page current. if param is empty, set current is 1
String selPage = (String) request.getParameter("selPage");
if("".equals(selPage) || selPage == null){
selPage = "1";
}
//get all list actor
studentList = studentDAO.getallUser();
//set paging for list actor
if (studentList != null && !studentList.isEmpty()) {
Pages page = new Pages();
page.setTotalSize(studentList.size());
page.setCurrPage(Integer.parseInt(selPage));
int min = page.minIndex();
int max = 0;
max = page.maxIndex(studentList.size());
request.setAttribute("studentList",studentList.subList(min, max));
request.setAttribute("page", page);
}
request.setAttribute("st",studentList);
return mapping.findForward("success");
}
}
File jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!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>This is all information</title>
</head>
<body>
<table class="table table-hover">
<tr>
<th>UserID</th>
<th>firstName</th>
<th>lastName</th>
<th>email</th>
<th>password</th>
<th></th>
</tr>
<logic:iterate name="studentList" id="studentListId">
<tr>
<td><bean:write name="studentListId" property="userId" /></td>
<td><bean:write name="studentListId" property="fistName" /></td>
<td><bean:write name="studentListId" property="lastName" /></td>
<td><bean:write name="studentListId" property="email" /></td>
<td><bean:write name="studentListId" property="password" /></td>
</tr>
</logic:iterate>
</table>
</body>
</html>
Nhưng khi chạy thì nó ra lỗi Cannot find bean: “studentList” in any scope. Nhờ các bạn giúp
Bài liên quan
có thấy FileUploadForm của chú đâu?
Đã edit nhé. Nhờ bạn giúp.
Chú map jsp với uploadForm mà lại sử dụng studentlist trong Action?
trong action chú phải như này:
FileUploadForm fileUploadForm = (FileUploadForm ) form; và cái list của chú năm trong form này chứ
Hi bác. Mình map với cả 2 mà. Map với file /pages/studentList.jsp là với action action.StudentListPageAction mà. Con upload thì mình sử lý đc rồi