1. The first type:
Recent projects using Struts2 (in fact, I think the background check, especially the struts of the check, can be placed in other places, such as to JS or business logic), and the system just also used the ExtJS, when the problem arises: if you submit data, Struts check does not pass, then struts will automatically return input, and ExtJS submit data is Ajax, regardless of the return value of struts, then the system will be error, the page can not display the information about the failure of the verification,
In this case, one way to do this is to add the input result set to the Struts.xml, redirect to another action handler, and then return the results of the AJAX request to the page, but the page does not display information about the validation failure. How can I resolve the details of the page display check failure?
Check the Internet, found struts2 new version of the Interceptor support to return the result set of the preprocessing listener function (for example: After the action method returns an input, I can intercept in the interceptor you return to the input view), we only need to implement this interface, You can then add a listener to the interceptor.
Look at the code:
[Java]View PlainCopyprint?
- Public class Exceptioninterceptor extends Abstractinterceptor {
- private static final Logger Logger = loggerfactory
- . GetLogger ("Exceptioninterceptor");
- @Override
- Public String Intercept (actioninvocation invocation) {
- try {
- //Increased monitoring
- Invocation.addpreresultlistener (new Mystrutslistener ());
- String retstring = Invocation.invoke ();
- return retstring;
- } catch (Exception e) {
- Logger.error (Stacktrace.getexceptiontrace (e));
- //to-do
- }
- }
- }
- //Implementation interface: Preresultlistener
- class Mystrutslistener implements Preresultlistener {
- @Override
- public void Beforeresult (actioninvocation actioninvocation, String result) {
- //Filter the request of result to input view
- if (Result! = null && result.equals ("input")) {
- //Set struts return value to null
- Actioninvocation.setresultcode (null);
- //Get fielderror error check information from Action
- Actioncontext Actioncontext = actioninvocation
- . Getinvocationcontext ();
- Valuestack VC = Actioncontext.getvaluestack ();
- map<string, object> ferrors = (map<string, object>) VC
- . Findvalue ("fielderrors");
- String returnmessage = null;
- for (map.entry<string, object> entry:ferrors.entrySet ()) {
- ArrayList list = (ArrayList) entry.getvalue ();
- if (list = null && list.size () > 0) {
- ReturnMessage = List.get (0). toString ();
- Break ;
- }
- }
- //Page write back JSON
- try {
- HttpServletResponse response = (httpservletresponse) actioncontext
- . GetContext (). Get (
- Org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
- Response.setcontenttype ("Application/json; Charset=utf-8 ");
- Response.setheader ("Cache-control", "No-cache");
- PrintWriter writer = Response.getwriter ();
- Writer.print ("{success:false,message: '" + returnmessage + "'}");
- Writer.flush ();
- Writer.close ();
- } catch (IOException E1) {
- Logger.error (Stacktrace.getexceptiontrace (E1));
- }
- }
- }
- }
Finally, in the foreground parsing the received JSON data, feedback to the user, the effect and the struts failed to verify the message is the same. ^_^
Simple explanation: If you intercept input, remove a checksum from the struts stack and return to the page,
The above is the personal opinion, inevitably has the place which is ill-conceived
Loading source http://blog.csdn.net/jsjxieyang/article/details/8107547
---------------------------------------------------------------------------
The second type:
One: Define a result yourself
Java Code?
123456789101112131415161718192021222324252627282930313233343536 |
package
result;
import
java.io.PrintWriter;
import
java.util.Map;
import
org.apache.struts2.ServletActionContext;
import net.sf.json.JSONObject;
import
com.opensymphony.xwork2.ActionContext;
import
com.opensymphony.xwork2.ActionInvocation;
import
com.opensymphony.xwork2.Result;
import
com.opensymphony.xwork2.util.ValueStack;
public
class
validators_json
implements Result {
@SuppressWarnings
(
"unchecked"
)
@Override
public
void
execute(ActionInvocation arg0)
throws
Exception {
//获值栈中fieldErrors的值
ValueStack vc = ActionContext.getContext().getValueStack();
Map<String, String[]> ferrors = (Map<String,String[]>) vc.findValue(
"fieldErrors"
);
//获得输出流
ServletActionContext.getResponse().setCharacterEncoding(
"utf8"
);
ServletActionContext.getResponse().setContentType(
"text/html"
);
PrintWriter out = ServletActionContext.getResponse().getWriter();
//将map转换为json
JSONObject json =JSONObject.fromObject(ferrors);
//想客户端输出
System.out.println(json.toString());
out.print(json.toString());
out.close();
// for (Map.Entry<String, String[]> entry : ferrors.entrySet())
// System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
}
|
Two, then modify the configuration file Struts.xml
Add a definition for type
xml/html Code?
123 |
< result-types > < result-type name = "validators_json" class = "result.validators_json" default = "true" > </ result-type > </ result-types > |
Use
xml/html Code?
1 |
< result name = "input" type = "validators_json" ></ result > |
In this way, the message generated by the validation framework can be sent to the client in JSON mode, which can be defined by itself.
Implementation of AJAX+STRUTS2 validation framework asynchronous validation data.
When using the different commit, the background through the validation rules file, check does not pass, when jumping to input view, the foreground display error message solution