Small tail

In the previous article, there was a historical issue about the use of POST and GET modes in Struts2. Post mode can solve the Chinese garble problem because the default encoding in Struts2 is UTF-8. In the figure below are the default values for constants in the Struts2 core jar package

When opened, we can see this property:

struts.i18n.encoding=UTF-8
Copy the code

In this case, Struts2 sets the default encoding format to UTF-8. Note that this is a pointer-to-POST submission; Then let’s review the GET and POST methods:

  • Get gets data from the server, and POST sends data to the server.
  • On the client side, get submits data through the URL, and the data can be seen in the URL. In POST mode, data is submitted in HTML headers
  • For GET, the server uses Request.QueryString to get variable values.For POST, the server uses Request.Form to get submitted data.
  • A maximum of 1024 bytes of data can be submitted in GET mode, but there is no such limit in POST mode
  • Security issues. As mentioned in 2, when you use GET, the parameters are displayed in the address bar, but post is not. So, if the data is Chinese and non-sensitive, use GET; If the data the user enters is not Chinese characters and contains sensitive data, use POST

If the encoding format of our JSP page is GBK, we will use post, because the two do not match, when we use GET, because the programmer (that is, we) did not set the encoding format for him, so there will be garbled

Advanced start – Struts2’s three ways of passing values

The three ways are:

  • Write properties directly in action (value model)
  • Create a new object class separately and declare it in action (domain model)
  • Model-driven action inherits ActionSupport to achieve Model-driven

The value model is the advanced part of the previous part;

The domain model is as follows

XML file, then configure the web. XML file, write JSP foreground file, this time we use the JSTLB tag to write, the key code is as follows:

Index.jsp imports the library file here as the login page:

<%@ taglib prefix="s" uri="/struts-tags"% >Copy the code

Design controls:

<s:form action="login" method="post">
		<s:textfield name="user1.uname" label="Username"></s:textfield>
		<s:textfield name="user1.pwd" label="Password"></s:textfield>
		<tr>
			<td><s:submit type="simple" value="Submit"></s:submit></td>
			<td><s:reset type="simple" value="Reset"></s:reset></td>
		</tr>
	</s:form>
Copy the code

Show.jsp is shown here as the login success page

hello,${user.uname }
Copy the code

Configure the struts2.xml file

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="hello.xml"/>

</struts>

Copy the code

This is the equivalent of a JSP include for easy development.

Configure the hello.xml file

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="login" />
        
        <action name="login" class="com.huaruan.action.UserAction">
            <result name="success">show.jsp</result>
            <result name="input" type="redirect">index.jsp</result>
        </action>
    </package>

</struts>
Copy the code

In an XML file, all packages must inherit from the Struts-default class, which is a feature of Struts2

Write the UserAction class

public class UserAction extends ActionSupport{
	private User user1;
	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}
	public String execute() {
		String result = null;
		if("Chuxus".equals(user1.getUname())&&"123".equals(user1.getPwd())) {
			result=SUCCESS;
		}else {
			result = INPUT;
		}
		return result;
	}
Copy the code

This is done by separating the get and set methods from the value mode, creating a private variable of type User in the Action class, and making a set&GET method for it. The rest is the same as before. The main point is the details in the JSP page:

  • The name values of the two controls are user1, which we created in the action class, and.user(the specific property).

Finally, write the User class

 public class User {
	private String uname;
	private String pwd;
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd; }}Copy the code

This completes the use of the domain mode to pass values

The model driver is as follows:

The preparation steps are basically the same as above. The following are the main differences

  • One difference is that the Action class no longer provides get&set methods for variables of type User and instantiates them directly
  • The Action class implements the ModelDriven interface
  • The execute() method is no longer needed in the class; it must be removed and its own method added
  • In the FORM form of a JSP page, the action property value changes from UserAction to UserAction! [Name of your method]
  • Modify this in struts. XML
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
Copy the code

In this way, we have completed the exploration of three different modes of transmission value, if you have any questions, you can leave a message below oh!