Action get form submission data
In the previous web stage, submit the form to the servlet, and use the method in the request object to obtain, getParameter and getParameterMap.
Submit the form to action, but action does not have a request object and cannot use the request object directly.
There are three main ways to obtain form submission data: action.
- Use ActionContext class
- Using the ServletActionContext class
- Use interface injection mode
Use ActionContext class to get
- Map < string, Object > getparameters(): returns an object containing all HttpServletRequest parameter information. Because the method is not a static method, you need to create an ActionContext class object. This ActionContext class object is not new.
- static ActionContext getContext(): gets the ActionContext object of the current thread.
1. Create form: form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath }/form.action" method="get"> username:<input type="text" name="username"/> <br/> password:<input type="text" name="password"/> <br/> address:<input type="text" name="address"/> <br/> <input type="submit" value="Submission"/> </form> </body> </html>
2. Submit the form to action: struts 2.xml
<package name="demo" extends="struts-default" namespace="/"> <action name="form" class="action.LoginAction"></action> </package>
3. Use ActionContext to get data in action: LoginAction.java
public class LoginAction extends ActionSupport { // The first way is to use ActionContext class to get @Override public String execute() throws Exception { // 1 get ActionContext object ActionContext context = ActionContext.getContext(); // 2 call the method to get the form data // key is the value of the name attribute of the form input item, and value is the entered value. Map<String, Object> map = context.getParameters(); Set<String> keys = map.keySet(); for (String key : keys) { // Get value from key // Array form: because there may be a check box in the input item Object[] obj = (Object[]) map.get(key); System.out.println(Arrays.toString(obj)); } return NONE; } }
Use ServletActionContext class to get
- static HttpServletRequest getRequest(): gets the HttpServletRequest object of the Web application.
- static HttpServletResponse getResponse(): gets the HttpServletResponse object of the Web application.
- static ServletContext getServletContext(): get the ServletContext object of the Web application.
- static PageContext getPageContext(): get the PageContext object of the Web application.
Call the static method in the class to get the request object
public class LoginAction extends ActionSupport { // The second way is to use the ServletActionContext class to get @Override public String execute() throws Exception { // 1 use ServletActionContext to get request object HttpServletRequest request = ServletActionContext.getRequest(); // 2 call the method in request to get the result String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); System.out.println(username + ":" + password + ":" + address); //Operate on three domain objects //1 request domain HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("req", "reqValue"); //2 session domain HttpSession session = request.getSession(); session.setAttribute("sess", "sessValue"); //3 ServletContext domain ServletContext context = ServletActionContext.getServletContext(); context.setAttribute("contextname", "contextValue"); return NONE; } }
Using interface injection
- ServletRequestAware: the Action that implements this interface can directly access the HttpServletRequest instance of the Web application.
- ServletResponseAware: the Action that implements this interface can directly access the HttpServletResponse instance of the Web application.
- SessionAware: the Action that implements this interface can directly access the HttpSession instance of the Web application.
- ServletContextAware: the Action that implements this interface can directly access the ServletContext instance of the Web application.
Let action implement the interface to get the request object
public class LoginAction extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } @Override public String execute() throws Exception { String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); System.out.println(username + ":" + password + ":" + address); //Operate on three domain objects //1 request domain HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("req", "reqValue"); //2 session domain HttpSession session = request.getSession(); session.setAttribute("sess", "sessValue"); //3 ServletContext domain ServletContext context = ServletActionContext.getServletContext(); context.setAttribute("contextname", "contextValue"); return NONE; } }