在这里,我们将给大家分享关于处理JSP页面,数组列表时发生异常的知识,让您更了解处理jsp页面,数组列表时发生异常怎么办的本质,同时也会涉及到如何更有效地AddWithValue参数为NULL时发生异
在这里,我们将给大家分享关于处理JSP页面,数组列表时发生异常的知识,让您更了解处理jsp页面,数组列表时发生异常怎么办的本质,同时也会涉及到如何更有效地AddWithValue参数为NULL时发生异常、Ajax程序:处理异步调用中的异常(在客户端调用Web Service代理时发生异常的处理办法)、asp.net – 处理您的请求时发生异常.此外,执行自定义错误页面时发生另一个异常、Code First 初始化数据时发生异常的内容。
本文目录一览:- 处理JSP页面,数组列表时发生异常(处理jsp页面,数组列表时发生异常怎么办)
- AddWithValue参数为NULL时发生异常
- Ajax程序:处理异步调用中的异常(在客户端调用Web Service代理时发生异常的处理办法)
- asp.net – 处理您的请求时发生异常.此外,执行自定义错误页面时发生另一个异常
- Code First 初始化数据时发生异常
处理JSP页面,数组列表时发生异常(处理jsp页面,数组列表时发生异常怎么办)
我无法弄清楚这个问题。
我的错误:
org.apache.jasper.JasperException:第28行处理JSP页面/index.jsp时发生异常
Line 28: <c:forEach items="${data.visit}" var="visit">
Java类:
public class DataBean implements Serializable,ServletContextListener {
private static final String nameOfLogger = DataBean.class.getName();
private static final Logger logger = Logger.getLogger(nameOfLogger);
public class Visit {
public Visit(String dateOfTheVisit,String category,String idClient,String idInsrurer,String idDoctor,String idVisit,String accepted) {
this.dateOfTheVisit = dateOfTheVisit;
this.category = category;
this.idClient = idClient;
this.idInsrurer = idInsrurer;
this.idDoctor = idDoctor;
this.idVisit = idVisit;
this.accepted = accepted;
}
public String getIdVisit() {
return idVisit;
}
public void setIdVisit(String idVisit) {
this.idVisit = idVisit;
}
public String getDateOfTheVisit() {
return dateOfTheVisit;
}
public void setDateOfTheVisit(String dateOfTheVisit) {
this.dateOfTheVisit = dateOfTheVisit;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getIdClient() {
return idClient;
}
public void setIdClient(String idClient) {
this.idClient = idClient;
}
public String getIdInsrurer() {
return idInsrurer;
}
public void setIdInsrurer(String idInsrurer) {
this.idInsrurer = idInsrurer;
}
public String getIdDoctor() {
return idDoctor;
}
public void setIdDoctor(String idDoctor) {
this.idDoctor = idDoctor;
}
String idVisit;
String dateOfTheVisit;
String category;
String idClient;
String idInsrurer;
String idDoctor;
String accepted;
public String getAccepted() {
return accepted;
}
public void setAccepted(String accepted) {
this.accepted = accepted;
}
}
public class Insurer {
public Insurer(String idInsurer,String name) {
this.idInsurer = idInsurer;
this.name = name;
}
public String getIdInsurer() {
return idInsurer;
}
public void setIdInsurer(String idInsurer) {
this.idInsurer = idInsurer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String idInsurer;
String name;
}
//public List<Visit> visitArray = new ArrayList<>();
public List<Insurer> insurerArray = new ArrayList<>();
private java.sql.Connection psqlCon = null;
private boolean psqlConnectionCreated = false;
synchronized public ArrayList<Visit> getVisit() throws ClassNotFoundException,SQLException {
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","password");
Statement stm;
stm = conn.createStatement();
String sql = "Select * From Customer";
ResultSet rst;
rst = stm.executeQuery(sql);
ArrayList<Visit> visitArray = new ArrayList<>();
while (rst.next()) {
Visit visit = new Visit(rst.getString("dateOfTheVisit"),rst.getString("category"),rst.getString("idClient"),rst.getString("idInsurer"),rst.getString("idDoctor"),rst.getString("idVisit"),rst.getString("accepted"));
visitArray.add(visit);
}
return visitArray;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods,choose Tools | Templates.
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods,choose Tools | Templates.
}
}
jsp:
<%@page import="java.util.List"%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<jsp:useBean id="data"scope="application"/>
<H1>The tableName Database Table </H1>
<TABLE BORDER="1">
<TR>
<TH>Date</TH>
<TH>Category</TH>
<TH>IdClient</TH>
<TH>IdInsurer</TH>
<TH>IdDoctor</TH>
<TH>Accepted</TH>
<TH>ID</TH>
</TR>
<tbody>
<c:forEach items="${data.visit}" var="visit">
<tr>
<td><c:out value="${visit.dateOfTheVisit}"/></td>
<td><c:out value="${visit.category}"/></td>
<td><c:out value="${visit.idClient}"/></td>
<td><c:out value="${visit.idInsrurer}"/></td>
<td><c:out value="${visit.idDoctor}"/></td>
<td><c:out value="${visit.idVisit}"/></td>
<td><c:out value="${visit.accepted}"/></td>
</tr>
</c:forEach>
</tbody>
</TABLE>
</BODY>
</HTML>
我不确定我做错了什么,但是我认为这与bean的定义有关。
我感谢您的每一次尝试!
AddWithValue参数为NULL时发生异常
我有以下代码用于指定SQL查询的参数。我在使用时遇到了异常Code 1
;但是当我使用时工作正常Code 2
。在这里,Code2
我们检查是否为null,因此是否为if..else
块。
例外:
参数化查询’{@application_ex_id nvarchar(4000))SELECT E.application_ex_id
A’期望参数’@application_ex_id’未提供。
代码1 :
command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
代码2 :
if (logSearch.LogID != null){ command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);}else{ command.Parameters.AddWithValue("@application_ex_id", DBNull.Value );}
题
您能否解释一下为什么它无法从代码1中的logSearch.LogID值中获取NULL(但能够接受DBNull)?
有更好的代码来处理吗?
参考 :
- 将空值分配给SqlParameter
- 返回的数据类型因表中的数据而异
- 从数据库smallint到C#可为空的int的转换错误
- DBNull的意义是什么?
码
public Collection<Log> GetLogs(LogSearch logSearch) { Collection<Log> logs = new Collection<Log>(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string commandText = @"SELECT * FROM Application_Ex E WHERE (E.application_ex_id = @application_ex_id OR @application_ex_id IS NULL)"; using (SqlCommand command = new SqlCommand(commandText, connection)) { command.CommandType = System.Data.CommandType.Text; //Parameter value setting //command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID); if (logSearch.LogID != null) { command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID); } else { command.Parameters.AddWithValue("@application_ex_id", DBNull.Value ); } using (SqlDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { Collection<Object> entityList = new Collection<Object>(); entityList.Add(new Log()); ArrayList records = EntityDataMappingHelper.SelectRecords(entityList, reader); for (int i = 0; i < records.Count; i++) { Log log = new Log(); Dictionary<string, object> currentRecord = (Dictionary<string, object>)records[i]; EntityDataMappingHelper.FillEntityFromRecord(log, currentRecord); logs.Add(log); } } //reader.Close(); } } } return logs; }
答案1
小编典典烦人的,不是吗。
您可以使用:
command.Parameters.AddWithValue("@application_ex_id", ((object)logSearch.LogID) ?? DBNull.Value);
或者,使用“ dapper”之类的工具,它将为您解决所有麻烦。
例如:
var data = conn.Query<SomeType>(commandText, new { application_ex_id = logSearch.LogID }).ToList();
我 很想 在dapper中添加一个方法来获取IDataReader
…尚不确定是否是个好主意。
Ajax程序:处理异步调用中的异常(在客户端调用Web Service代理时发生异常的处理办法)
一个好的Web应用程序,强大的异常处理是少不了的。最近在学习Ajax的过程中,我总结了几种不同情况下的处理异步调用中异常的不同办法;想分享给大家;
1,在客户端调用Web Service代理时发生异常的处理办法
2,使用Asp.NetAjax内建的异常处理方法
3,Web.Config中customError节中设置错误跳转页
此篇博客总结的是:在客户端调用Web Service代理时发生异常的处理方法。
由于Ajax的异步效果,再加上后台的运行,让我们很难判断某次对服务器的请求是否顺利完成,浏览器对Ajax程序运行时发生的异常无能为力;
Ajax的使用,使Web应用程序从客户端想服务器端发起异步Http协议请求,在理想情况下是没有问题的,但是Web程序的运行并不是一直顺利的,它有着很多不确定性——网络故障、。。。开发者的粗心大意,一旦出现问题就会导致异步请求失败。
在开发中提高性能,尽量避免这些异常,但是有些异常是避免不了的,这样的话,我们就该处理这些异常。
当然,Asp.Net Ajax也考虑到了这些,并提供了一整套针对异步回调过程中发生的异常处理机制;
进入主题:在客户端调用Web Service代理时发生异常的处理办法;
客户端调用Web Service代理的语法:
[NameSpace].[ClassName].[MethodName](param1,param2,...,onSuceeded,onFailed)
——其中:onSuceeded:成功时调用的函数;
onFailed:失败时调用的函数;(本博客介绍的重点之处)
onFailed回调函数会接收一个类型为Sys.Net.WebServiceError的参数,表示异常对象。如下:
FunctiononFailed(error){
//显示异常信息
//处理异常信息
}
注:
ASP.NET AJAX的客户端Sys.Net.WebServiceError类型封装了异步请求服务器时可能发生的异常,它提供了若干个只读的属性,提供了对异常信息的详细描述。Sys.Net.WebServiceError类型的属性如下(在接下来的例子将会用到这些属性):
属 性 |
描 述 |
exceptionType |
获取服务器端异常的具体类型 |
message |
获取详细的异常描述信息 |
statusCode |
获取造成异常的HTTP响应的状态码 |
stackTrace |
获取服务器端异常的栈跟踪信息 |
timedOut |
获取一个布尔值,表示异常是否是由于网络连接超时造成的 |
例子:
服务器端:ErroHandling.asmx
定义了两个方法GetDivision(int a,int b):实现两个整数相除,并返回相除结果; int Timeout():
<%@ WebService Language="C#"%>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Threading;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ErrorHandling : System.Web.Services.WebService
{
[WebMethod]
public int GetDivision(int a,int b)
{
return a / b;
}
[WebMethod]
public int Timeout()
{
Thread.Sleep(5000);
return 0;
}
}
(注意:Web Service类要添加[ScriptService]标记;在方法上标记[WebMethod]表示:服务器端释放该方法,客户端可访问服务器端的该方法)
客户端调用服务器端的GetDivision(int a,int b)函数,并传入两个参数:2,0;程序借助Asp.Net Ajax异步通信层将参数发送至服务器,服务器完成计算后将结果返回至客户端;
显然,因为输入的被除数为0,服务器端执行时必定要抛出异常;但是
并没有在服务器端做异常处理,此时客户端也许会显示异常错误,但是如果客户端并没有显示异常错误,那么客户端的用户就要无限期的等下去,可见是多么的不合理;那么如何在客户端处理此类异常?
新建Asp.Net页面5_ClientProxy.aspx;前台添加ScriptManager控件(
和UpdatePanel控件联合使用可以实现页面异步局部更新的效果),及ErrorHandling.asmx的引用:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ErrorHandling.asmx" />
</Services>
</asp:ScriptManager>
添加两个按钮,并且分别在单击事件中调用getDivision(int a,int b)和timeout()方法:(注:getDivision()函数传入的参数为5,0:即被除数为0)
<input type="button" value="getDivision" onclick="getDivision(5,0)" />
<input type="button" value="timeout" onclick="timeout()" />
<script language="javascript" type="text/javascript">
ErrorHandling.set_timeout(2000);
//getDivision(int a,int b)方法:
function getDivision(a,b)
{
function getDivision(a,b)
{
ErrorHandling.GetDivision(a,b,succeededCallback,FailedCallback);
//调用服务器端GetDivision(int a,int b)函数,成功时的回调函数为succeededCallback(),失败时的回调函数为FailedCallback()
}
}
function timeout()
{
ErrorHandling.Timeout(succeededCallback,FailedCallback);
}
function succeededCallback(result)
{
alert(result)
}
function FailedCallback(error,userContext,methodName)
{
// ASP.NET AJAX的客户端Sys.Net.WebServiceError类型封装了异步请求服务器时可能发生的异常,它提供了若干个只读的属性,提供了对异常信息的详细描述。Sys.Net.WebServ- iceError类型的属性,如下:
//属 性 描 述
//exceptionType 获取服务器端异常的具体类型
//message 获取详细的异常描述信息
//statusCode 取造成异常的HTTP响应的状态码
//stackTrace 获取服务器端异常的栈跟踪信息
//timedOut 获取一个布尔值,表示异常是否是由于网络连接超时造成的
var message = String.format(
"Timeout: {0}\nMessage: {1}\nExceptionType: {2}\nStackTrace: {3}",error.get_timedOut(),error.get_message(),error.get_exceptionType(),error.get_stackTrace());
alert("Error at " + methodName + "\n\n" + message);
}
</script>
点击"getDivision"按钮:因为被除数为0,有异常,所以调用回调函数FailedCallback(),运行结果如下图;假如传入的参数为5,1,则调用succeededCallback()函数:
点击"timeout"按钮:因为睡眠时间为5秒,超过设定的2秒,所以所以调用回调函数FailedCallback(),运行结果如下图;假如设定6秒,则调用succeededCallback()函数:
总结:
本篇博客主要讲:在客户端调用Web Service代理时发生异常的处理办法,以及Sys.Net.WebServiceError类型的属性的使用
asp.net – 处理您的请求时发生异常.此外,执行自定义错误页面时发生另一个异常
Server Error in ‘/’ Application.
Runtime Error
Description: An exception occurred while processing your request. Additionally,another exception occurred while executing the custom error page for the first exception. The request has been terminated.
上面的错误现在发生在我的PROD上.回收应用程序池可以解决问题,但几天之后,同样的错误再次出现.我确实浏览了这个主题的其他帖子相关,但似乎总是为他们发生这个错误,这对我来说是不一样的.
解决方法
使用HTML错误页面,而不是.ASPX页面.
禁用自定义错误也会“修复”此问题,但是,您不希望在生产中执行此操作.
您还可以使用ELMAH记录异常,然后解决问题.
Code First 初始化数据时发生异常
问题重现
用 Entity Framework
的 Code First
默认生成的数据库文件被我直接删除了,
然后不管怎么重新编译等等,运行后总是会报错如下:
解决方案同下
Cannot attach the file *.mdf as database
今天关于处理JSP页面,数组列表时发生异常和处理jsp页面,数组列表时发生异常怎么办的介绍到此结束,谢谢您的阅读,有关AddWithValue参数为NULL时发生异常、Ajax程序:处理异步调用中的异常(在客户端调用Web Service代理时发生异常的处理办法)、asp.net – 处理您的请求时发生异常.此外,执行自定义错误页面时发生另一个异常、Code First 初始化数据时发生异常等更多相关知识的信息可以在本站进行查询。
本文标签: