我想知道在我的JSP项目中使用MVC的网络并找到了一个很好的讨论here,现在我有以下内容:
public class Subject {
private String subjectId = null;
private String subjectName = null;
public String getSubjectId() {
return subjectId;
}
public void setSubjectId(String subjectId) {
this.subjectId = subjectId;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
}
我的SubjectDAO
课程是:
public class SubjectDAO {
public List<Subject> subjectList() throws SQLException{
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Subject> subjects = new ArrayList<>();
try {
connection = DatabaseManager.initConnection();
statement = connection.prepareStatement(Constant.SUBJECT_QUERY);
resultSet = statement.executeQuery();
while (resultSet.next()) {
Subject subject = new Subject();
subject.setSubjectId(resultSet.getString("_id"));
subject.setSubjectName(resultSet.getString("subj_name"));
subjects.add(subject);
}
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
return subjects;
}
}
在我的ControllerServlet
我有:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
List<Subject> subjects = subjectDAO.subjectList();
request.setAttribute("subjects", subjects);
getServletContext().getRequestDispatcher("/content.jsp").forward(
request, response);
} catch (Exception e) {
throw new ServletException("Cannot obtain subjects from DB", e.getCause());
}
}
我的JSP视图是:
<c:forEach items="${subjects}" var="subject">
<li
<c:catch>
<c:choose>
<c:when test="${subject.subjectId == param.subj_id}">
<c:out value="class=\"selected\""/>
</c:when>
</c:choose>
</c:catch>
><a href='/programming-iqs/admin-controller?action=manage-content&subj_id=
<c:out value="${subject.subjectId}"/>'>
<c:out value="${subject.subjectId}"/>
</a>
</li>
</c:forEach>
使用上面的代码我得到了:
javax.servlet.ServletException: Cannot obtain subjects from DB
我无法解决此问题,非常感谢任何帮助。
在使用/实现MVC之前,我的JSP代码是:
<%
ResultSet sujectResult = DatabaseManager.getSubjects();
while (sujectResult.next()) { %>
<li <%
String selectedSubject = request.getParameter("subj_id");
try {
if (selectedSubject.equals(sujectResult.getString("_id"))) {%>
class="selected" <%}%>><a
href='/programming-iqs/admin-controller?action=manage-content&subj_id=
<%= sujectResult.getString("_id")%>'>
<%= sujectResult.getString("subj_name")%>
</a> <%
} catch (NullPointerException e) {
}
} %></li>
并且当然代码工作得非常好,现在请帮我实现MVC。 感谢
答案 0 :(得分:1)
java.lang.IllegalStateException: Cannot forward after response has been committed
例外的一般原因如下:
out.write("something")
例如: 这是正确的 -
if(condition1)
redirect to resource 1
else if(condition 2)
redirect to resource 2
else
redirect to resource 3
But this is wrong --
if(condition 1)
redirect to resource 1
again
redirect to resource 2
答案 1 :(得分:-1)
您可以添加e.printStackTrace();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
List<Subject> subjects = subjectDAO.subjectList();
request.setAttribute("subjects", subjects);
getServletContext().getRequestDispatcher("/content.jsp").forward(
request, response);
} catch (Exception e) {
e.printStackTrace();
throw new ServletException("Cannot obtain subjects from DB", e.getCause());
}
}
然后只需检查您的应用服务器控制台。它应该告诉你问题是什么。