今天碰到了一个非常小的问题,checkbox选中之后,提交到服务器端之后,仍然为false。真是找遍了各种解决方法。类似ispostback等等,我这个问题有些怪的,原来是好的,但不知道为什么不行了。
最后,把 CssClass去掉了,就好了。三个小时时间就这样过去了。哎
今天碰到了一个非常小的问题,checkbox选中之后,提交到服务器端之后,仍然为false。真是找遍了各种解决方法。类似ispostback等等,我这个问题有些怪的,原来是好的,但不知道为什么不行了。
最后,把 CssClass去掉了,就好了。三个小时时间就这样过去了。哎
服务器端的WebService类,同时也是客户端类(因为有main方法以及测试的代码)
package webservice;
import javax.xml.namespace.QName;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeader;
import org.apache.axis.message.SOAPHeaderElement;
public class Power
{
public static String GetMessage()
{
return "调用成功!";
}
public static void main(String... a) throws Exception
{
String endpoint = "http://localhost:9000/hg/services/Power";
SOAPHeaderElement headerElement = new SOAPHeaderElement("http://schemas.xmlsoap.org/soap/envelope/", "sessionInfo");
headerElement.setMustUnderstand(false);
headerElement.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
SOAPElement sid = headerElement.addChildElement("sessionId");
sid.addTextNode("1234567890ABCDEF1234567890ABCDEF");
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("GetMessage", "GetMessage"));
// call.addParameter(new QName("http://cn.zuoguodang.service", "name"),
// org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.SOAP_STRING);
// 设定 Header
call.addHeader(headerElement);
System.out.println(call.invoke(new Object[] {}));
}
}
服务器端的SoapHeader类
package webservice;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.message.MessageElement;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.message.SOAPHeaderElement;
public class Soapheader extends org.apache.axis.handlers.BasicHandler
{
private String sessionId;
@Override
public void invoke(MessageContext msgContext) throws AxisFault
{
boolean processedHeader = false;
String headerName = null;
try
{
// 取得 Request 的 SOAP 信息
Message msg = msgContext.getRequestMessage();
SOAPEnvelope envelope = msg.getSOAPEnvelope();
SOAPHeader header = envelope.getHeader();
Iterator it = header.examineAllHeaderElements();
SOAPHeaderElement hel;
while (it.hasNext())
{
hel = (SOAPHeaderElement) it.next();
headerName = hel.getNodeName();
if (headerName.equals("sessionInfo"))
{
// 对于 mustUnderstand 设为 true 的 Header,必须
// 利用下列的方式把它设为"已经处理",否则 service
// 会回传 Did not understand "MustUnderstand"
hel.setProcessed(true);
checkUser(hel);
processedHeader = true;
}
}
}
catch (SOAPException e)
{
throw new AxisFault("无法处理 SOAP Header.", e);
}
if (!processedHeader) { throw new AxisFault("接收 SOAP Header 失败"+headerName); }
}
private void checkUser(SOAPHeaderElement hel) throws AxisFault
{
MessageElement fele = hel.getChildElement(new QName("", "sessionId"));
if (fele == null) { throw new AxisFault("找不到 sessionId 标签."); }
sessionId = fele.getValue();
if (!"12345".equals(sessionId)) { throw new AxisFault("认证失败"); }
}
}
因为是使用axis2 webservice,所以要修改server-config.wsdd,增加SoapHeader作为handler,放在根节点下面
<handler name="Authenticate" type="java:webservice.Soapheader" />
并在globalConfiguration/requestFlow下面再增加节点:
<handler type="Authenticate"/>
c# 来调用这个服务的话,先生成代理类,然后在生成的代理类里面增加一个属性:
public CredentialSoapHeader SoapHeaderValue;
然后在需要调用的webserice方法上加注释:
[System.Web.Services.Protocols.SoapHeaderAttribute("SoapHeaderValue")]
CredentialSoapHeader类代码如下:
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "")]
[System.Xml.Serialization.XmlRootAttribute("sessionInfo", Namespace = "", IsNullable = false)]
public class CredentialSoapHeader : System.Web.Services.Protocols.SoapHeader
{
public string sessionId;
}
调用webservice并使用soapheader的代码如下:
PowerService sps = new PowerService();
CredentialSoapHeader cs = new CredentialSoapHeader();
cs.sessionId = "12345";
sps.SoapHeaderValue = cs;
Response.Write(sps.GetMessage());
在做好VISIO后,需要另存为JPEG格式,如果一页一页的导,很麻烦,用VBA实现是最快的,另存为的JPEG文件名称为页名称。
Sub AutoSave2JPG()
Dim fileName
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "\/|\\|:|\*|\?|\""|<|>|\|"
For i = 1 To Application.ActiveDocument.Pages.Count Step 1
fileName = Application.ActiveDocument.Pages(i).Name
fileName = reg.Replace(fileName, "")
fileName = Application.ActiveDocument.Path & "\" & fileName & ".jpg"
Application.ActiveDocument.Pages(i).Export fileName
On Error Resume Next
Next
End Sub
重新安装:msxml6.msi,
下载地址:http://download.microsoft.com/download/b/7/1/b71d5305-618d-4b82-858b-386db3cc4453/msxml6.msi
五 6
Posted by 寻道者 in .Net方面 | No Comments
使用NHibernate时,多表级联更新时遇到这个错误:object references an unsaved transient instance – save the transient instance before flushing
解决方法:在<set>中,加入及联更新 cascade=”all”
在开发Web项目过程中,经常需要使用dropdownlist联动,那么在asp.net mvc dropdownlist联动是如何实现的呢?下面我一步一步的介绍如何实现。
首先,在Controller类中实现两个方法,一个是页面刚加载时候的Index(),另一个是dropdownlist onchange()。实现代码如下:
public ActionResult Index(int? projectId, int? teamId)
{
//获取所有的项目ID
ViewData["projectinfos"] = new SelectList(db.FindAllProjectInfos(), "ProjectId", "ProjectName");
int _teamId = teamId ?? 0;
int _projectId = projectId ?? 0;
//通过项目ID获取班组ID
ViewData["teams"] = new SelectList(db.FindAllTeamsByProjectId(_projectId), "teamid", "teamname");
if (_teamId != 0)//不为0,表示要获取指定班组下的信息
{
var contracts = db.FindTeamContractInfosByForeignerId(_teamId);
return View(contracts);
}
else//为0,表示要获取所有班组下的信息
{
var contracts = db.FindTeamContractInfos();
return View(contracts);
}
}
///
/// 通过项目ID获取班组ID
///
public JsonResult GetTeamsByProjectId(int id)
{
var teams = db.FindAllTeamsByProjectId(id);
return Json(teams.ToList(), JsonRequestBehavior.AllowGet);
}
接下来,在View页面中加入两个dropdownlist和javascript脚本。实现代码如下:
<div class="editor-label">
所属项目名称
<%: Html.DropDownList("projectId", ViewData["projectinfos"] as SelectList, "---全部项目---")%>
所属班组名称
<%: Html.DropDownList("teamId", ViewData["teams"] as SelectList, "---全部项目---")%>
<input type="submit" value="查询" />
</div>
<script type="text/javascript">// <![CDATA[
$(document).ready(function () {
$("#projectId").change(function () { GetTeams() });
});
function GetTeams() {
$("#teamId").empty();
var url = "/ContractInfo/GetTeamsByProjectId/" + $("#projectId").val() + "/";//规则是控制器/方法/参数
alert(url);
$.getJSON(url, function (data) {
$.each(data, function (i, item) {
$("
<option></option>
")
.val(item["TeamId"])
.text(item["TeamName"])
.appendTo($("#teamId"));
});
});
}
// ]]></script>
最后就是运行一下看看效果。这里有几个点要注意:
有需求要将几个操作oracle数据库的方法写到一个事务里面,用到了TransactionScope对象。但使用的时候不是很顺利,先是说不能嵌套事务,仔细检查原来的代码中,的确有事务,那个现在要把几个操作放到一个事务里面,只写删除了单个方法的事务。随之又报了另一个错误。
using (TransactionScope scope = new TransactionScope())
{
do_ora1();
do_ora2();
do_ora3();
scope.Complete();
}
[DllNotFoundException: 无法加载 DLL“oramts.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。] System.Data.OracleClient.OracleInternalConnection.Enlist(String userName, String password, String serverName, Transaction transaction, Boolean manualEnlistment) +157 System.Data.OracleClient.OracleInternalConnection.Activate(Transaction transaction) +68 System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction) +33 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +1318 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +100 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +116 System.Data.OracleClient.OracleConnection.Open() +40
在网上查原因,各式各样的都有,我是这样的解决的:
我的oracle版本是10.2,我下载了一个oramts10.dll文件,将这个文件重命名为oramts.dll后复制到oracle\product\10.2.0\db_1\BIN下。就OK了。有些人说下载msvcrtd.dll到system32下面,我试了是没有的。
解决方法:
就是在Linq to SQL Designer中,把一些字段的UpdateCheck属性设置为Never,这样,这些字段在更新的时候,就不会再出现在Where条件中了。其实比较推荐的做法,就是在表中设立主键,因为更新的时候,只要把主键作为Where条件,就可以单独的确立一行数据了。把除主键外的字段属性中UpdateCheck设置为Never即可。
在开发c/s程序的时候,如果用到了微软企业库做为数据库连接,那么就在要app.config中写入明文的数据库连接字串,这样是很不安全的。
在发现此问题后,我看一下enterprise lib的源代码,修改了一个类后,就解决了这个问题。
该类就是ConnectionString
首先在类里面加入一个解密方法:
/// <summary>
/// 进行DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的以Base64</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>已解密的字符串。</returns>
public string Decrypt(string pToDecrypt, string sKey)
{
if ((pToDecrypt.Length % 4) != 0)//因为加密后是base64,所以用4来求余进行验证
{
return pToDecrypt;
}
if (pToDecrypt.Contains("Password"))//如果包含Password,表示没有加密
{
return pToDecrypt;
}
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
然后修改ConnectionString类的构造方法,增加一行代码:
this.connectionString = Decrypt(connectionString,"abcdefgh");
此后,对数据库连接字串的加密方法是:
/// <summary>
/// 进行DES加密
/// </summary>
/// <param name="pToEncrypt">要加密的字符串</param>
/// <param name="sKey">密钥,且必须8位</param>
/// <returns>以Base64格式返回的加密字符串</returns>
public string Encrypt(string pToEncrypt, string sKey)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
}
You are currently browsing the archives for the .Net方面 category.
Arclite theme by digitalnature | powered by WordPress