Struts learning 1 - first experience download of struts2

1. First experience of Struts2
2. Access to ServletAPI
3. Data verification
4. UI label

5. General label

https://github.com/fulq1234/mystudy/tree/master/struts

1. First experience of Struts2




The basic steps of struts2 development program

1. Load the struts2 class library


2. Configure web.xml
<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern><!-- Target all requests to the specified filter-->
</filter-mapping>


3. Develop the view layer page
<%@taglib prefix="s" uri="/struts-tags" %>
<div>
	<h1><s:property value="message"/><!-- output display statement--></h1>
</div>
<div>
	<form action="login.action" method="post">
		please enter your name:
		<input name="name" type="text"><br/>
		please enter your password
		<input name="password" type="password"/><br/>
		<input type="submit" value="提交"/>
	</form>
</div>


4. Develop control layer Action
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action {
	private String name = "";
	private String password = "";		
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		//Save parameters to session
		Map<String,Object> session = ActionContext.getContext().getSession();
		session.put("name", name);//Display ${sessionScope.name} in the foreground		
		if(name.equals("jason") && password.equals("2010")){
			return "success";
		}else{
			return "fail";
		}
	}
	//... ...getter/setter methods
}


5. Configure struts.xml
<?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>
	<!-- Create a default package, inherited from struts-default package of Struts2-->
	<package name="default" namespace="/" extends="struts-default">
		<!-- Receive and process the user's /helloWorld.action request,
			 And according to the return result, complete the jump -->
		<action name="helloWorld" class="cn.intro.action.HelloWorldAction"><!-- name corresponds to the action attribute value of the form -->
			<result name="success">/helloWorld.jsp</result><!-- Success returns a string corresponding to Action -->
		</action>
		<action name="login" class="cn.intro.action.LoginAction"><!-- name corresponds to the action attribute value of the form -->
			<result name="success">/success.jsp</result><!-- Success returns a string corresponding to Action -->
			<result name="fail">/fail.jsp</result>
		</action>
	</package>
</struts>


6. Deploy, run the project


Perform process analysis



2. Access ServletAPI

Struts2 accesses Servlet API
1. Decoupling
2. Coupling

1. Decoupling The encapsulation
of the Servlet API
    provides access to three Map objects, corresponding to the request, session, and application scopes. Obtain these three Map objects
through the ActionContext class
(1) Object get("request")
(2) Map getSession()
(3) Map getApplication()


Map<String,Object> session = ActionContext.getContext().getSession();
session.put(CURRENT_USER,name);

The page shows ${sessionScope.CURRENT_USER}

Example

Map<String,Object> session = ActionContext.getContext().getSession();
session.put("name", name);//Display ${sessionScope.name} in the foreground



Access mode coupled with Session Obtain Servlet API object
through ServletActionContext
1.ServletContext getServletContext()
2.HttpServletResponse getResponse()
3.HttpServletRequest getRequest( ) 4.Get
session object through request.getSession()

Via xxx.setAttribute() and xxx.getAttribute()

HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute(CURRENT_USER,name);
HttpSession session2 = ServletActionContext.getRequest().getSession();
session2.setAttribute("name", name);//Display ${sessionScope.name} in the foreground

3. Data verification


validate() intercept method
public class LoginAction extends ActionSupport{
	//... ...omitting the code
	public void validate(){
		if(this.getUsername().length() == 0){
			addFieldError("name","Username cannot be empty");
		}
		if(this.getPassword().length() ==0){
			addFieldError("pwd","User password cannot be empty");
		}
	}
	//... ...omitting the code
}


As long as ActionSupport is inherited and the validate method is written, the validate method will be executed before the execute method. If an error is found (addFieldError), then an "input" view is returned, which must be executed. Otherwise, continue with subsequent methods.
If the verification error information will be output on the page?
Use the label provided by struts2 in the view page
1.fielderror
2.actionerror
<%@taglib prefix="s" uri="/struts-tags"%>
<s:fielderror/>
<s:fielderror fieldName="name"/>

4. UI label

struts2 UI labels


UI tags, common form tags (ognl)
<s:form>...</s:form> Form tags
<s:textfield>...</s:textfield> Text input boxes
<s:password>... </s:password> Password input box
<s:textarea>...</s:textarea> Text box input box
<s:radio>...</s:radio> Radio button
<s:checkbox>. ..</s:checkbox> Multi-select box
<s:select/> Drop-down option box
<s:submit/> Submit label
<s:reset/> Reset label
<s:hidden/> Hide field label
Support theme style


and Compared with ordinary html, it is an ongl expression.
<s:form action="login.action" method="post">
	<s:textfield name="name" value="xx" label="用户名"></s:textfield>
	<s:password name="password" label="密码"></s:password>
	<s:submit value="提交"/>
</s:form>
label is the option name, theme is the theme, the default theme is simple, other examples are xhtml


UI labels, commonly used non-form labels
<s:actionerror/> show Action error
<s:actionmessage/> show Action message
<s:fielderror/> show Field error
ctrl+alt+T Find classes in jar packages

5. General label

Conditional tags
<s:if>... ...</s:if>
<s:elseif>... ...</s:elseif>
<s:else>... ...</s :else>
Iterative tags
<s:iterator>... ...</s:iterator>


<s:if test="expression">
Code to execute
</s:if>
<s:elseif test=" Expression">
Code to be executed
</s:elseif>
<s:else>
Code to be executed
</s:else> For


example
<s:if test="sex==1">先生</s:if>

<s:iterator value="collection object" status="status" id="name">
Read the collection object and output it
</s:iterator>
value attribute: the collection object that needs to be traversed
status attribute: the current iteration element IteratorStatus instance
id attribute: the id of the current iteration element, which can directly access the element, this parameter is optional

Example :
<s:iterator value="users" status="st">
	<s:property value="user"><br/>
	<!-- Why can you also output user that? Because properties are placed in a value stack, s:property takes the first element -->
	<s:property/>
</s:iterator>

Common tags, commonly used data tags
property Output the value of the ongl expression
debug For debugging, you can view the content in the ActionContext
Action Allows to call Action
bean directly in the JSP page Used to instantiate a JavaBean object
include Similar to <jsp:include> in JSP The tag
param is used as a subtag of other tags
push pushes the value to the top of the value stack
set assigns a variable to the specified scope (request, session... ...)
url is used to create a URL

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325954270&siteId=291194637