Struts2+jsp页面选择框,实现两个动态下拉框联动,下拉框选项从数据库得出,jsp和action怎么写

Struts2+jsp页面选择框,实现两个动态下拉框联动,下拉框选项从数据库得出,jsp和action怎么写,第1张

第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:
LIBEXPORT_API int mySum(int a,int b){ return a+b;}
C# 导入定义:
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAuto,CallingConvention=CallingConventionStdCall)]
public static extern int mySum (int a,int b);
}
在C#中调用测试:
int iSum = RefCommmySum(,);
运行查看结果iSum为5,调用正确。第一步试验完成,说明在C#中能够调用自定义的动态链接库函数。
第二步,我定义了字符串 *** 作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:
LIBEXPORT_API char mySum(char a,char b){sprintf(b,"%s",a); return a;}
C# 导入定义:
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAuto,
CallingConvention=CallingConventionStdCall)]
public static extern string mySum (string a, string b);
}
在C#中调用测试:
string strDest="";
string strTmp= RefCommmySum("45", strDest);
运行查看结果 strTmp 为"45",但是strDest为空。我修改动态链接库实现,返回结果为串b:
LIBEXPORT_API char mySum(char a,char b){sprintf(b,"%s",a) return b;}
修改 C# 导入定义,将串b修改为ref方式:
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAuto,CallingConvention=CallingConventionStdCall)]
public static extern string mySum (string a, ref string b);
}
在C#中再调用测试:
string strDest="";
string strTmp= RefCommmySum("45", ref strDest);
运行查看结果 strTmp 和 strDest 均不对,含不可见字符。再修改 C# 导入定义,将CharSet从Auto修改为Ansi:
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAnsi,CallingConvention=CallingConventionStdCall)]
public static extern string mySum (string a, string b);
}
在C#中再调用测试:
string strDest="";
string strTmp= RefComm mySum("45", ref strDest);
运行查看结果 strTmp 为"45",但是串 strDest 没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。再次修改 C# 导入定义,将串b修改为引用(ref):
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAnsi,CallingConvention=CallingConventionStdCall)]
public static extern string mySum (string a, ref string b);
}
运行时调用失败,不能继续执行。
第三步,修改动态链接库实现,将b修改为双重指针:
LIBEXPORT_API char mySum(char a,char b){sprintf((b),"%s",a); return b;}
C#导入定义:
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAnsi,CallingConvention=CallingConventionStdCall)]
public static extern string mySum (string a, ref string b);
}
在C#中调用测试:
string strDest="";
string strTmp= RefComm mySum("45", ref strDest);
运行查看结果 strTmp 和 strDest 均为"45",调用正确。第三步实现了函数出口参数正确输出结果。
第四步,修改动态链接库实现,实现整数参数的输出:
LIBEXPORT_API int mySum(int a,int b,int c){ c=a+b; return c;}
C#导入的定义:
public class RefComm
{
[DllImport("LibEncryptdll",
EntryPoint=" mySum ",
CharSet=CharSetAnsi,CallingConvention=CallingConventionStdCall)]
public static extern int mySum (int a, int b,ref int c);
}
在C#中调用测试:
int c=0;
int iSum= RefComm mySum(,, ref c);
运行查看结果iSum 和c均为5,调用正确。
经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。
三、结论
在 C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。
对于函数返回值,C# 导入定义和 C++ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 CharSet 和 CallingConvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 C# 程序的目录下即可,我这里是一个 C# 的动态链接库,两个动态链接库就在同一个目录下运行。

<%@ page language="java" import="javautil" pageEncoding="UTF-8"%>
<%@ page import="javautil" %>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'MyJspjsp' starting page</title>

<meta >两种方案

一个是用JavaScript,一次性将两个select的数据统统取出来,将第二张表的数据放到数组中,通过第一个的change事件来改变,优点,运行速度快,缺点,需编写对应的javascript代码,且无法反映实时数据

第二种方案是每次都提交,进行联动,优点,能反映数据库实时数据信息,无须写javascript,缺点,运行速度相对慢,且需要考虑保存用户的已经填写的其他表单数据

看我的,方便

<form name="form1" method="post">
<td width="40%">
<select>
<%
try{
Connection con=jgetConnection();
Statement statement=concreateStatement(ResultSetTYPE_SCROLL_SENSITIVE,ResultSetCONCUR_UPDATABLE);
String sql="select from table";
ResultSet r=statementexecuteQuery(sql);
while(rnext())
{
%>
<option value=<%=rgetString(1)%>><%=rgetString(1)%></option>
<%}
jreleaseConnection(con);
}catch(SQLException ee){outprint("数据库连接失败!");}

%>
</select>
</td>
</form>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="javasql"%>
<%@ page import="javaio"%>
<jsp:useBean id="user" class="comconn_" scope="page"/>
<%
String las =requestgetParameter("la");
String ars =requestgetParameter("ar");
%>
<html>
<head>
<meta >我给你看看我的做法吧,你可以参考下:
<%
JDBCOperator jdbcOper = null; //定义jdbc连接
JDBCResultSet rs = null;
String strSql = "select value,name from temp";//定义sql语句

jdbcOper = new JDBCOperator();
rs = jdbcOperrunQuery(strSql);//执行sql语句
int rowcount = rsgetRowCount();//取条数
for (int i=0;i<rowcount;i++)
{
outprintln("<option value='"+rsgetCell(i,0)+“‘”>"+rsgetCell(i,1)+"</option>");
}
%>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13399919.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存