方法表
HTML页面
<td style="width:18%;" >
<a class="btn btn-primary fa fa-user" th:onclick="'javascript:scriptSetting.showMore('+${dataItem.id}+');'">测试</a>
</td>
js 中的代码,因为填写参数使用的弹出框,所以弹出框的提交按钮直接写到了一个方法里
showMore:function(id){
var url=__ctx+"/platform/script/scriptSetting/getMethodParam?methodId="+id;
DialogUtil.open({
type:2,
area: ['50%', '50%'],
title: '填写参数信息及结果返回',
content:url,
btn:[{
label: '提交',
iconCls:'btn btn-success fa fa-ok',
action: function(dialog,index1) {
var url=__ctx+"/platform/script/scriptSetting/getMethodParam";
// ajaxSubmit
var form =DialogUtil.getChildFrameWindow(index1).methodParamForm;
form.submit();
// $(DialogUtil.getChildFrameWindow(index1).methodParamForm).submit({
// form.submit({
// type: 'post',
// url:url,
// success: function(dialog,index1){
// DialogUtil.alert("参数传递成功");
// DialogUtil.getChildFrameWindow(index1).location.reload();
//
// // window.location.reload();
// // DialogUtil.closeAll();
// },
// error: function(XmlHttpRequest, textStatus, errorThrown){
// DialogUtil.alert("参数填写失败");
// } });
}
}, {
label: '取消',
iconCls:'btn btn-danger fa fa-cancel',
action: function(dialog,index) {
window.location.reload();
DialogUtil.close(index);
}
}]
});
},
注释掉的代码,是因为点击提交以后页面不会刷新
Controller
@RequestMapping("getMethodParam")
public ModelAndView getMethodParam(HttpServletRequest request, HttpServletResponse response,ScriptSetting scriptSetting) throws Exception
{
ModelAndView mv=getAutoView(request);
Long id=RequestUtil.getLong(request, "methodId",0L);
Map<String, Object> methodParam = null;
if(scriptSetting.getId() != null){
methodParam = scriptSettingService.getMethodResult(scriptSetting);
}else if(id!=0L){
methodParam = scriptSettingService.getMethodParam(id);
}
return mv.addObject("methodParam",methodParam)
.addObject("id",id);
}
service .主要是使用bean的名字得到这个类的class,因为如果直接实例化的话,方法里面调用的其他的方法可能会报 空指针异常。
/**
* 得到方法需要的参数
* @param id
* @return
*/
@Override
public Map<String, Object> getMethodParam(Long id) {
Map<String, Object> map = new HashMap();
int type = 3;//1代表返回参数,2代表返回结果,3代表没有这个方法,4是类不存在
map.put("type",type);
ScriptSetting sc=this.getById(id);
String clsName =sc.getClassName() ;
String methodName = sc.getMethodName();
// String oriName = clsName .substring(clsName .lastIndexOf(".")+1);
Object bean = applicationContext.getBean(toLowerCaseFirstOne(clsName));
String name1 = bean.getClass().getName();
clsName = name1;
try{
//实体化这个类,能够在没有参数的时候直接调用
Class clazz = Class.forName(clsName);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if(method.getName().equals(methodName)){
//获取本方法所有参数类型,存入数组
Class<?>[] getTypeParameters = method.getParameterTypes();
Parameter[] params = method.getParameters();
List<String> paramName = new ArrayList<>();
for (Parameter param : params) {
paramName.add(param.getName());
}
//如果没有参数就直接返回方法的返回值
if(getTypeParameters.length==0){
type = 2;
Object obj = Class.forName(clsName).newInstance();
Object result = method.invoke(bean);
String json = JSON.toJSONString(result);
map.put("type",type);
map.put("json",json);
}else{//如果有参数就返回参数类型
type = 1;
map.put("type",type);
int i=1;
for (Class<?> class1 : getTypeParameters) {
String parameterName = class1.getName();
String name = (String) paramName.get(i-1);
parameterName = parameterName+" "+name;
map.put("p"+i,parameterName);
i++;
}
}
}
}
}catch (InvocationTargetException e){
type = 6;
map.put("type",type);
}catch (Exception e){
type = 4;
map.put("type",type);
}
return map;
}
/**
* 得到方法结果
* @param scriptSetting
* @return
*/
@Override
public Map<String, Object> getMethodResult(ScriptSetting scriptSetting) {
Long id = scriptSetting.getId();
Map<String, Object> map = new HashMap();
int type = 2;//1代表返回参数,2代表返回结果,3代表没有这个方法,4是类不存在
map.put("type",type);
ScriptSetting sc=this.getById(id);
String clsName =sc.getClassName() ;
String methodName = sc.getMethodName();
Object bean = applicationContext.getBean(toLowerCaseFirstOne(clsName));
String name1 = bean.getClass().getName();
clsName = name1;
try{
//实体化这个类,能够在没有参数的时候直接调用
Class clazz = Class.forName(clsName);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if(method.getName().equals(methodName)){
//获取本方法所有参数类型,存入数组
Class<?>[] getTypeParameters = method.getParameterTypes();
Object[] inArgsBefore = this.putEntityIntoObject(scriptSetting,getTypeParameters.length);
Object[] inArgAfter = this.typeParse(inArgsBefore, getTypeParameters);
// Object obj = Class.forName(clsName).newInstance();
if(BeanUtils.isNotEmpty(method.invoke(bean,inArgAfter))){
Object result = method.invoke(bean,inArgAfter);
String json = JSON.toJSONString(result);
map.put("json",json);
}
else{
map.put("json","结果为空");
}
}
}
}catch (InvocationTargetException e){
type = 7;
map.put("type",type);
e.printStackTrace();
}
catch (Exception e){
type = 5;
map.put("type",type);
e.printStackTrace();
}
return map;
}
private Object[] putEntityIntoObject(ScriptSetting ss,int l){
Object[] inArgs = new Object[l] ;
inArgs[0] = ss.getP1();
if(ss.getP2() != null){
inArgs[1] = ss.getP2();
}else{
return inArgs;
}
if(ss.getP3() != null){
inArgs[2] = ss.getP2();
}else{
return inArgs;
}
if(ss.getP4() != null){
inArgs[3] = ss.getP2();
}else{
return inArgs;
}
if(ss.getP5() != null){
inArgs[4] = ss.getP2();
}else{
return inArgs;
}
if(ss.getP6() != null){
inArgs[5] = ss.getP2();
}else{
return inArgs;
}
if(ss.getP7() != null){
inArgs[6] = ss.getP2();
}else{
return inArgs;
} if(ss.getP8() != null){
inArgs[7] = ss.getP2();
}else{
return inArgs;
} if(ss.getP9() != null){
inArgs[8] = ss.getP2();
}else{
return inArgs;
} if(ss.getP10() != null){
inArgs[9] = ss.getP2();
}else{
return inArgs;
}
return inArgs;
}
private Object[] typeParse(Object[] inArgsBefore,Class<?>[] getTypeParameters){
Object[] inArgs = new Object[getTypeParameters.length] ;
int i=0;
for (Class<?> getTypeParameter : getTypeParameters) {
inArgs[i] = this.alltypeParse(inArgsBefore[i], getTypeParameter);
i++;
}
return inArgs;
}
private Object alltypeParse(Object o,Class<?> getTypeParameter){
Object res = new Object();
if(getTypeParameter.getName().toLowerCase().contains("int".toLowerCase())){
Integer p = Integer.parseInt((String)o);
return p;
}else if(getTypeParameter.getName().toLowerCase().contains("boolean".toLowerCase())){
boolean p = Boolean.parseBoolean((String)o);
return p;
}else if(getTypeParameter.getName().toLowerCase().contains("byte".toLowerCase())){
Byte p = Byte.parseByte((String) o);
return p;
}else if(getTypeParameter.getName().toLowerCase().contains("short".toLowerCase())){
short p = Short.parseShort((String) o);
return p;
}else if(getTypeParameter.getName().toLowerCase().contains("long".toLowerCase())){
Long p = Long.parseLong((String) o);
return p;
}else if(getTypeParameter.getName().toLowerCase().contains("double".toLowerCase())){
Double p = Double.parseDouble((String) o);
return p;
}else if(getTypeParameter.getName().toLowerCase().contains("float".toLowerCase())){
Float p = Float.parseFloat((String) o);
return p;
}
else if(getTypeParameter.getName().toLowerCase().contains("string".toLowerCase())){
String p = (String) o;
return p;
}
return res;
}
//首字母转小写
public static String toLowerCaseFirstOne(String s){
if(Character.isLowerCase(s.charAt(0)))
return s;
else
return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
}
public ScriptSetting test5(Long id){
return this.getById(id);
}
填写方法参数,以及方法结果返回的界面
<!--
time:2020-12-01 14:06:12
desc:edit the anga_script_setting
-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:insert="~{templates/hcommon :: h-common}">
</head>
<body>
<div class="x_panel boxscroll">
<div th:if="${methodParam.type}==4">
类不存在
</div>
<div th:if="${methodParam.type}==7">
因为参数输入问题,导致方法内部异常(比如输入的ID不存在)
</div>
<div th:if="${methodParam.type}==5">
类型填写错误
</div>
<div th:if="${methodParam.type}==6">
方法内部有异常
</div>
<div th:if="${methodParam.type}==3">
方法不存在
</div>
<div th:if="${methodParam.type}==2">
结果
<p th:text="${methodParam.json!=null}?${methodParam.json}"></p>
</div>
<div th:if="${methodParam.type}==1">
<form class="form-horizontal" id="methodParamForm" action="getMethodParam" method="post">
<div class="form-group" th:each="map:${methodParam}" th:if="${map.key}!= type">
<label th:text="${map.value}" class="red" />
<input type="text" class="form-control" id="name" th:name="${map.key}" placeholder="请输入符合类型的参数" th:value />
</div>
<input type="hidden" name="id" th:value="${id}" />
</form>
</div>
</div>
<div th:insert="~{templates/fcommon :: f-common}"></div>
<script type="text/javascript" th:src="@{/js/platform/scriptSetting.js}"></script>
</body>
</html>