Commonly used Web elements include: request, session, application, etc., and we generally use sessions more. How to access web elements in Struts2? This is very important because it can complete the data interaction between the program background and the user. The following is a registration example to demonstrate its process:
1. index.jsp file
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!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"> <base href="<%=basePath %>"/> <title>Insert title here</title> </head> <body> <h1>Demo</h1> <form action="user/user02!register" method="post"> Name: <input type="text" name="user.name"></input> <br/> Password: <input type="text" name="user.password"></input> <br/> <input type="submit" value="register"/> </form> </body> </html>
The function is very simple - that is, the user enters the user name and password, and then it can be obtained in the background, and then it will be displayed to the user after the registration is successful.
2. Struts.xml configuration
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="front" namespace="/user" extends="struts-default"> <action name="user*"> <result>/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
There are two ways to complete this function
3. The first type (UserAction01)
package com.myservice.web; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction01 extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private User user; private Map request; private Map session; private Map application; public UserAction01(){ request = (Map)ActionContext.getContext().get("request"); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); } public String register(){ request.put("name", user.getName()); request.put("password", user.getPassword()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }This method is to use the ActionContext.getContext() method to obtain the context, and then get the request, session and application
4. Another method (UserAction02) is very common and very famous - Ioc (control inversion) and DI (dependency injection). It requires the implementation of 3 interfaces as follows:
package com.myservice.web; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class UserAction02 extends ActionSupport implements RequestAware, SessionAware,ApplicationAware{ private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String register(){ request.put("name", user.getName()); request.put("password", user.getPassword()); return SUCCESS; } @Override public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this.application = application; } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } @Override public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this.request = request; } }This implements a function - put the user's name and password into the request, and when using it, we just need to take it out.
5. Success.jsp takes out the contents of the request and displays it
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!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> <h3>Successful registration</h3> <s:property value="#request.name"/>Registered successfully, with the password: <s:property value="#request.password"/> </body> </html>
The results are shown as:
The above is all the content of accessing web elements in Struts2. I hope you can give you a reference and I hope you can support Wulin.com more.