The third day of Struts 2 study - saving login information and data validation

Keywords: Java Session Struts JSP Attribute

In JSP, the scope of server-side data storage is usually request, session and application, and their corresponding Servlet API s are HttpServlet Rquerst, HttpSession and ServletContext, respectively. These scopes also need to be accessed in the controller of Struts2.

Struts2 provides an ActionContext class, which is called the Action Context or Action Environment, through which Action can access the most commonly used Servlet API.

The Servlet API has multiple access modes:

1. Servlet API decoupled access, using ActionContext to access Servlet API

Common methods are:

getContext(): Static method to get the current ActionContext instance.

getSession(): Returns a Map object that simulates session scope.

getApplication(): Returns a Map object that simulates the application scope.

get(String key): Passing in the "request" parameter to this method, you can return an Object object that simulates the request scope.

getParamenters(); Returns a Map object that holds the parameters uploaded by the browser. The code is as follows:

 1         //Get context
 2         ActionContext context=ActionContext.getContext();
 3         //Relation request Scope data
 4         Map requestMap=(Map)context.get("request");
 5         requestMap.put("req_user",users);
 6         //Save in session Range
 7         Map sessionMap=context.getSession();
 8         sessionMap.put("session_user", users);
 9         //Preserved on a global scale
10         Map appMap=context.getApplication();
11         appMap.put("app_user", users);        

 

Accessing Servlet API in IoC

In the above code, request, session and application are all scoped objects that are acquired by the Action class itself. This method is characterized by the fact that the code for acquiring and using objects is placed in a class. In addition, there is a method that only the code for using these objects is retained in the Action class, and the code for acquiring objects is implemented by Struts 2. Struts 2 takes these objects and injects them into the Action class, which enables them to be used. This idea of implementation is known as IoC. It can separate the code of acquiring object and using object, and make the system decouple and combine.

The code is as follows:

 1 public class UserAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
 2 
 3     private Map<String, Object>request;
 4     private Map<String, Object>session;
 5     private Map<String, Object>application;
 6 
 7     @Override
 8     public void setApplication(Map<String, Object>application) {
 9         // TODO Auto-generated method stub
10         this.application=application;
11     }
12     @Override
13     public void setSession(Map<String, Object> session) {
14         // TODO Auto-generated method stub
15         this.session=session;
16     }
17     @Override
18     public void setRequest(Map<String, Object> request) {
19         // TODO Auto-generated method stub
20         this.request=request;
21     }
22 }

In the above code, Action implements RequestAware, Session Aware, and Application Aware interfaces, so that Struts 2 can inject request, session, application objects into the Action. Take session as an example, Struts 2 takes session object. When the UserAction object is created, Struts 2 determines whether the UserAction implements the Session Aware interface, and if it does, calls Useraction.

setSession () method, and session as a parameter into the method, the method's "this.seesion=session" code will save the session as a member variable, so as to achieve the injection of session object.

3. Access mode of Servlet API coupling

Obtain Servlet API objects through the ServletActionContext class

Strutes2 provides Servlet ActionContext to retrieve the original Servlet API

      ServletActionContext  getServletContext();

 

      HttpServletResponse  getResponse();

 

      HttpServletRequest   getRequest();    

Get the session object through request.getSession()

 

Set values through xxx.setAttribute()

The code is as follows:

1         HttpServletRequest request=ServletActionContext.getRequest();
2         HttpServletResponse response=ServletActionContext.getResponse();
3         HttpSession session=request.getSession();
4         ServletContext application=ServletActionContext.getServletContext();
5         request.setAttribute("req_user", users);
6         session.setAttribute("session_user", users);
7         application.setAttribute("app_user",users);

  

Data Check of Struts 2

Inheriting ActionSupport Class to Complete Action Development

The ActionSupport class not only implements the Action interface simply, but also adds support for validation and localization.

If an error occurs, the result input page will be looked up. If not, 404 will be reported.

        <action name="UserAction" class="com.house.action.UserAction" method="reg">
            <result name="show" type="dispatcher">/ch01/show.jsp</result>
            <result name="input">/ch01/reg.jsp</result>
        </action>
public class HouserUserAction extends ActionSupport{
//...Ellipsis code
        public void validate(){
            if(uname==null||uname.equals("")){
            super.addFieldError("uname_err","User name must be filled in");
            }
            if(upwd==null||upwd.equals("")){
                super.addFieldError("upwd_err","Password must be filled in");
            }
        }
}

HTML displays the imported struts 2 tag <%@ taglib uri="/struts-tags" prefix="s"%>

<s:fielderror fieldName="uname_err"/> Displays error information. If the name attribute is not set, all error information will be output and because the struts tag is not fiter filtered and the jsp page is not recognized, it must be configured /* filtered in web.Xml.

The Struts tag will have its own style that you can set, or the struts. ui. theme cancel style in Struts

Posted by ElizaLeahy on Tue, 09 Apr 2019 13:24:31 -0700