前端代碼:
{
xtype:'combo',
triggerAction: 'all',
emptyText:'輸入模號',
selectOnFocus:true,
editable:true,
typeAhead: true,
fieldLabel: '模號',
name: 'mold_id',
hiddenName: 'mold_id',
displayField: 'value',
valueField: 'value',
allowBlank: false,
maxHeight: 600,
listWidth: 100,
anchor:'95%',
lastQuery: '',
store: new Ext.data.ArrayStore({
url : 'mold_new_followAction.do?actionType=get_mold_list',
fields : ['value'], //後臺返回數組,前端接收值
listeners: {
exception : function(misc){
form.getForm().findField('mold_id').setValue('');
}
},
autoLoad : true
})
},{
xtype:'combo',
fieldLabel:'對比模號',
id:'compare_mold_id',
allowBlank:false,
typeAhead: true,
triggerAction:'all',
displayField:'value',
valueField:'value',
anchor:'95%',
value:'',
lastQuery: '',
store: new Ext.data.ArrayStore({
url : 'mold_new_followAction.do?actionType=get_mold_list',
fields : ['value'],
listeners: {
exception : function(misc){
Ext.getCmp('compare_mold_id').setValue('');
}
},
autoLoad : true
})
}
後臺代碼:
public ActionForward get_mold_list(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws
IOException, ServletException, SQLException {
String mold_id = httpServletRequest.getParameter("query"); //這個是combobox自帶的參數(取控件中輸入的值)
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
DataSource datasource = null;
ServletContext context = null;
context = servlet.getServletContext();
datasource = (DataSource) context.getAttribute(Globals.DATA_SOURCE_KEY);
String json = "";
String tempsql = "";
try {
conn = datasource.getConnection();
stmt = conn.createStatement();
tempsql = "select mold_id from mold with (nolock) where mold_id like '%"+mold_id+"%' "+
" union select mold_id from tsql.gdberp.dbo.mold with (nolock) where mold_id like '%"+mold_id+"%'";
//System.out.println(tempsql);
rs = stmt.executeQuery(tempsql);
while (rs.next()) {
json += "['" + rs.getString("mold_id")+ "'],";
}
if (json.length() > 0) {
json = json.replace("null", "");
json = json.substring(0, json.length() - 1);
json = "[" + json + "]";
} else {
json = "{\"success\":\"false\",\"message\":\"no result\"}";
}
//System.out.println(json);
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
try {
httpServletResponse.setContentType("text/json; charset=utf-8");
httpServletResponse.getWriter().write(json);
}
catch (IOException ex) {
ex.printStackTrace();
}
return actionMapping.findForward("");
}