Struts 2 - 意外异常捕获设置 'xx'
Struts 2 - Unexpected Exception caught setting 'xx'
我正在尝试将来自 HTML 表单的未知数字输入提交给操作 class 并使用操作中的请求方法(使用的 ServletActionContext)获取输入参数信息 class。
但它投掷
Unexpected Exception caught setting 'xx' on 'class classname Error setting expression 'xx' with value ['yy', ]
由于表单中的输入元素是使用 JS 动态添加的,因此我无法在 Action 中使用 getter 和 setter class。
如何无一例外地处理 Actionclass?
JSP:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Team Activity Log</title>
<SCRIPT language="javascript">
function addRow(tableid)
{
var table = document.getElementById(tableid);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("select");
element1=document.getElementById("sele").cloneNode(true);
element1.type="select";
cell1.appendChild(element1);
var cell2=row.insertCell(1);
var element2 = document.createElement("input");
element2.type="text";
element2.setAttribute("placeholder","E.g:1234");
cell2.appendChild(element2);
var cell3=row.insertCell(2);
var element3 = document.createElement("input");
element3.type="text";
element3.setAttribute("placeholder","Brief your work");
cell3.appendChild(element3);
var cell4=row.insertCell(3);
var element4 = document.createElement("input");
element4.type="text";
element4.setAttribute("placeholder","MM min");
cell4.appendChild(element4);
var cell5=row.insertCell(4);
var element5 = document.createElement("img");
element5.src="close.png";
element5.setAttribute("id","delete");
element5.setAttribute("onclick","changeImage(this)");
cell5.appendChild(element5);
}
function changeImage(temp)
{
(temp.parentElement).parentElement.remove();
}
function convert(tableid,temp2)
{
var table=document.getElementById(tableid);
console.log(table.rows.length);
var hidelement=document.getElementById(temp2);
hidelement.setAttribute("value",table.rows.length-1);
for(var i=1;i<table.rows.length;i++)
{
var tt="tt"+i;
var rf="rf"+i;
var des="des"+i;
var eff="eff"+i;
console.log("executing function");
var elemen=table.rows[i].cells[0].querySelector("*");
elemen.setAttribute("name",tt);
elemen=table.rows[i].cells[1].querySelector("*");
elemen.setAttribute("name",rf);
elemen=table.rows[i].cells[2].querySelector("*");
elemen.setAttribute("name",des);
elemen=table.rows[i].cells[3].querySelector("*");
elemen.setAttribute("name",eff);
}
}
</script>
</head>
<body>
<INPUT type="button" value="Add Row" onclick="addRow('matrix')" />
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:url id="myActionUrl" action="timesheetprocess" />
<form action=<s:property value="%{myActionUrl}" />>
<input type="hidden" id="taskcount" value="5">
<table id="matrix">
<tr>
<th>Task Type</th>
<th>Reference ID</th>
<th>Description</th>
<th>Efforts</th>
<th></th>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr><tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
</table>
<INPUT type="button" value="Submit" onclick="convert('matrix','taskcount')" />
</form>
</body>
</html>
struts.xml
中的操作标签
<action name="timesheetprocess" class="com.App.Controller.Timesheet.Timeprocess"
method="execute">
<result name="success">Time.jsp</result>
</action>
我的操作class:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class Timeprocess {
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
String tt="tt";
String rf="rf";
String des="des";
String eff="eff";
for(int i=0;i<Integer.parseInt(request.getParameter("taskcount"));i++)
{
System.out.println("Task Type :"+ request.getParameter(tt+String.valueOf(i)));
System.out.println("Task Type :"+ request.getParameter(rf+String.valueOf(i)));
System.out.println("Task Type :"+ request.getParameter(des+String.valueOf(i)));
System.out.println("Task Type :"+ request.getParameter(eff+String.valueOf(i)));
}
return "success";
}
}
您可以 Struts2 通过覆盖操作配置中的拦截器来控制参数填充过程。
<action name="timesheetprocess" class="com.App.Controller.Timesheet.Timeprocess"
method="execute">
<interceptor-ref name="defaultStack">
<param name="params.excludeParams">.*</param>
</interceptor-ref>
<result name="success">Time.jsp</result>
</action>
您修改 params
拦截器的 excludeParams
属性 以排除所有参数通过 OGNL 进行处理。
您应该不会以这种方式得到任何异常,但请确保您已配置 struts.devMode=false
<constant name="struts.devMode" value="false" />
它应该从输出中删除不必要的警告。如果您仍然在日志中收到任何警告或异常,那么您应该配置您的日志记录框架以抑制输出中的这些警告或错误。
我正在尝试将来自 HTML 表单的未知数字输入提交给操作 class 并使用操作中的请求方法(使用的 ServletActionContext)获取输入参数信息 class。
但它投掷
Unexpected Exception caught setting 'xx' on 'class classname Error setting expression 'xx' with value ['yy', ]
由于表单中的输入元素是使用 JS 动态添加的,因此我无法在 Action 中使用 getter 和 setter class。
如何无一例外地处理 Actionclass?
JSP:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Team Activity Log</title>
<SCRIPT language="javascript">
function addRow(tableid)
{
var table = document.getElementById(tableid);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("select");
element1=document.getElementById("sele").cloneNode(true);
element1.type="select";
cell1.appendChild(element1);
var cell2=row.insertCell(1);
var element2 = document.createElement("input");
element2.type="text";
element2.setAttribute("placeholder","E.g:1234");
cell2.appendChild(element2);
var cell3=row.insertCell(2);
var element3 = document.createElement("input");
element3.type="text";
element3.setAttribute("placeholder","Brief your work");
cell3.appendChild(element3);
var cell4=row.insertCell(3);
var element4 = document.createElement("input");
element4.type="text";
element4.setAttribute("placeholder","MM min");
cell4.appendChild(element4);
var cell5=row.insertCell(4);
var element5 = document.createElement("img");
element5.src="close.png";
element5.setAttribute("id","delete");
element5.setAttribute("onclick","changeImage(this)");
cell5.appendChild(element5);
}
function changeImage(temp)
{
(temp.parentElement).parentElement.remove();
}
function convert(tableid,temp2)
{
var table=document.getElementById(tableid);
console.log(table.rows.length);
var hidelement=document.getElementById(temp2);
hidelement.setAttribute("value",table.rows.length-1);
for(var i=1;i<table.rows.length;i++)
{
var tt="tt"+i;
var rf="rf"+i;
var des="des"+i;
var eff="eff"+i;
console.log("executing function");
var elemen=table.rows[i].cells[0].querySelector("*");
elemen.setAttribute("name",tt);
elemen=table.rows[i].cells[1].querySelector("*");
elemen.setAttribute("name",rf);
elemen=table.rows[i].cells[2].querySelector("*");
elemen.setAttribute("name",des);
elemen=table.rows[i].cells[3].querySelector("*");
elemen.setAttribute("name",eff);
}
}
</script>
</head>
<body>
<INPUT type="button" value="Add Row" onclick="addRow('matrix')" />
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:url id="myActionUrl" action="timesheetprocess" />
<form action=<s:property value="%{myActionUrl}" />>
<input type="hidden" id="taskcount" value="5">
<table id="matrix">
<tr>
<th>Task Type</th>
<th>Reference ID</th>
<th>Description</th>
<th>Efforts</th>
<th></th>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr><tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
<tr>
<td>
<select id="sele">
<option value="" disabled selected>Select your option</option>
<option value="SR">SR</option>
<option value="CR">CR</option>
<option value="ALM">ALM</option>
<option value="INCIDENT">INCIDENT</option>
<option value="OTHER">OTHER</option>
</select></TD>
<td>
<INPUT type="text" placeholder="E.g:1234"/>
</td>
<td>
<INPUT type="text" placeholder="Brief your work"/>
</td>
<td>
<INPUT type="text" placeholder="MM min" />
</td>
<td>
<img id="delete" src="close.png" onclick="changeImage(this)">
</td>
</tr>
</table>
<INPUT type="button" value="Submit" onclick="convert('matrix','taskcount')" />
</form>
</body>
</html>
struts.xml
中的操作标签<action name="timesheetprocess" class="com.App.Controller.Timesheet.Timeprocess"
method="execute">
<result name="success">Time.jsp</result>
</action>
我的操作class:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class Timeprocess {
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
String tt="tt";
String rf="rf";
String des="des";
String eff="eff";
for(int i=0;i<Integer.parseInt(request.getParameter("taskcount"));i++)
{
System.out.println("Task Type :"+ request.getParameter(tt+String.valueOf(i)));
System.out.println("Task Type :"+ request.getParameter(rf+String.valueOf(i)));
System.out.println("Task Type :"+ request.getParameter(des+String.valueOf(i)));
System.out.println("Task Type :"+ request.getParameter(eff+String.valueOf(i)));
}
return "success";
}
}
您可以 Struts2 通过覆盖操作配置中的拦截器来控制参数填充过程。
<action name="timesheetprocess" class="com.App.Controller.Timesheet.Timeprocess"
method="execute">
<interceptor-ref name="defaultStack">
<param name="params.excludeParams">.*</param>
</interceptor-ref>
<result name="success">Time.jsp</result>
</action>
您修改 params
拦截器的 excludeParams
属性 以排除所有参数通过 OGNL 进行处理。
您应该不会以这种方式得到任何异常,但请确保您已配置 struts.devMode=false
<constant name="struts.devMode" value="false" />
它应该从输出中删除不必要的警告。如果您仍然在日志中收到任何警告或异常,那么您应该配置您的日志记录框架以抑制输出中的这些警告或错误。