当我编写代码以上传文件并在按下jqGrid列后获取路径位置时,可以成功选择文件。但是,如果我按下添加按钮,则其行为就像它处于非活动状态一样。我的控制器里什么都没有。
这是我的jgGrid代码。我在script_loc列中执行文件uplaod:
jQuery("#grid").jqGrid({
url:'crud.do',
datatype: 'json',
mtype: 'GET',
colNames:['Seriel Number', 'Script Name', 'Script Location','Host Url','Protocol','Rampup Time','No of Users','Loop','Delay'],
colModel:[
{name:'sr_no',index:'sr_no', width:55,editable:false,editoptions:{readonly:true,size:10},hidden:true},
{name:'script_name',index:'script_name', width:100,editable:true, editrules:{required:false}, editoptions:{size:10, maxlength: 15}},
{name:'script_loc',index:'script_loc', width:100,editable:true, editrules:{required:false},edittype: 'file', editoptions:{enctype: "multipart/form-data"}},
{name:'host_url',index:'host_url', width:100,editable:true, editrules:{required:true}, editoptions:{ size:10, maxlength: 25}},
{name:'protocol',index:'protocol', width:100,editable:true, editrules:{required:true}, editoptions:{size:10, maxlength: 10}},
{name:'rampup_time',index:'rampup_time', width:100,editable:true, editrules:{required:true}, editoptions:{defaultValue: '0',size:2, maxlength: 4}},
{name:'noof_users',index:'noof_users', width:100,editable:true, editrules:{required:false}, editoptions:{size:4, maxlength: 8}},
{name:'loops',index:'loops', width:100,editable:true, editrules:{required:true}, editoptions:{defaultValue: '1' ,size:1, maxlength: 2}},
{name:'delay',index:'delay', width:100,editable:true, editrules:{required:true}, editoptions:{defaultValue: '0',size:1, maxlength: 4}}
],
postData: {
},
rowNum:20,
rowList:[20,40,60],
height: 200,
autowidth: true,
rownumbers: true,
pager: '#pager',
sortname: 'sr_no',
viewrecords: true,
sortorder: "asc",
caption:"Users",
emptyrecords: "Empty records",
loadonce: false,
loadComplete: function() {
},
})
答案 0 :(得分:0)
public class UserController {
protected static Logger logger = Logger.getLogger("controller");
@Autowired
private IUserService userService;
/**
* The default method when a request to /users is made.
* This essentially retrieves all users, which are wrapped inside a CustomUserResponse object.
* The object is automatically converted to JSON when returning back the response.
* The @ResponseBody is responsible for this behavior.
*/
@RequestMapping(value="/crud.do",method = RequestMethod.GET)
public @ResponseBody String getAll(@RequestParam("rows") int rows,
@RequestParam("page") int page,
@RequestParam("sidx") String sortColumn,
@RequestParam("sord") String sortOrder,HttpServletRequest request
) {
Enumeration requestParameters = request.getParameterNames();
while (requestParameters.hasMoreElements()) {
String element = (String) requestParameters.nextElement();
String value = request.getParameter(element);
if (element != null && value != null) {
logger.info("param Name : " + element
+ " value: " + value);
}
}
Gson myGson = new Gson();
logger.debug("Received request to get all users");
// Retrieve all users from the service
final int startIdx = (page - 1) * rows;
List<User> users = userService.getAll(startIdx,rows,sortColumn,sortOrder);
// Initialize our custom user response wrapper
CustomUserResponse response = new CustomUserResponse();
// Assign the result from the service to this response
response.setRows(users);
// Assign the total number of records found. This is used for paging
response.setRecords( String.valueOf(users.size()) );
// Since our service is just a simple service for teaching purposes
// We didn't really do any paging. But normally your DAOs or your persistence layer should support this
// Assign a dummy page
response.setPage( "1" );
// Same. Assign a dummy total pages
response.setTotal( "10" );
// Return the response
// Spring will automatically convert our CustomUserResponse as JSON object.
// This is triggered by the @ResponseBody annotation.
// It knows this because the JqGrid has set the headers to accept JSON format when it made a request
// Spring by default uses Jackson to convert the object to JSON
return myGson.toJson(response);
}
/**
* Edit the current user.
*/
@RequestMapping(value = "/add.do", method = RequestMethod.POST)
public @ResponseBody CustomGenericResponse add(
@RequestParam("script_name") String script_name,@RequestParam("script_loc") String script_loc,@RequestParam("host_url") String host_url,
@RequestParam("protocol") String protocol,@RequestParam("rampup_time") long rampup_time,
@RequestParam("noof_users") long noof_users,@RequestParam("loops") int loops,@RequestParam("delay") long delay
) {
logger.debug("Received request to add a new user");
String s= script_loc;
int l=loops;
System.out.println("loops"+s);
int max_srno=userService.getMaxSerielNo();
int new_sr_no=max_srno+1;
Boolean success = userService.add(new_sr_no,script_name,script_loc,host_url,protocol,rampup_time,noof_users,loops,delay);
// Check if successful
if ( success == true ) {
// Success. Return a custom response
CustomGenericResponse response = new CustomGenericResponse();
response.setSuccess(true);
response.setMessage("Action successful!");
return response;
} else {
// A failure. Return a custom response as well
CustomGenericResponse response = new CustomGenericResponse();
response.setSuccess(false);
response.setMessage("Action failure!");
return response;
}
}
@RequestMapping(value = "/edit.do", method = RequestMethod.POST)
public @ResponseBody CustomGenericResponse edit(
@RequestParam("script_name") String script_name,@RequestParam("script_loc") String script_loc,@RequestParam("host_url") String host_url,
@RequestParam("protocol") String protocol,@RequestParam("rampup_time") long rampup_time,
@RequestParam("noof_users") long noof_users,@RequestParam("loops") int loops,@RequestParam("delay") long delay,@RequestParam("id") String id
) {
int seriel_number=Integer.parseInt( id );
System.out.println("id--------------"+seriel_number);
logger.debug("Received request to update a new user");
Boolean success= userService.edit(script_name,script_loc,host_url,protocol,rampup_time,noof_users,loops,delay,seriel_number);
// Check if successful
if ( success == true ) {
// Success. Return a custom response
CustomGenericResponse response = new CustomGenericResponse();
response.setSuccess(true);
response.setMessage("Action successful!");
return response;
} else {
// A failure. Return a custom response as well
CustomGenericResponse response = new CustomGenericResponse();
response.setSuccess(false);
response.setMessage("Action failure!");
return response;
}
}
@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
public @ResponseBody CustomGenericResponse delete(
@RequestParam("id") String id,HttpServletRequest request
) {
Enumeration requestParameters = request.getParameterNames();
while (requestParameters.hasMoreElements()) {
String element = (String) requestParameters.nextElement();
String value = request.getParameter(element);
if (element != null && value != null) {
logger.info("param Name : " + element
+ " value: " + value);
}
}
logger.debug("Received request to delete an existing user");
int rowid=Integer.parseInt( id );
System.out.println("id==========="+id);
// Construct our user object. We just need the id for deletion.
// Assign the values from the parameters
// Do custom validation here or in your service
// Call service to add
Boolean success = userService.delete(rowid);
// Check if successful
if ( success == true ) {
// Success. Return a custom response
CustomGenericResponse response = new CustomGenericResponse();
response.setSuccess(true);
response.setMessage("Action successful!");
return response;
} else {
// A failure. Return a custom response as well
CustomGenericResponse response = new CustomGenericResponse();
response.setSuccess(false);
response.setMessage("Action failure!");
return response;
}
}
}