Introduction to Java from Introduction to Abandon: Basic Data Transfer in Struts 2

Comparing this with JSP's data transmission mode, you will find that... can really write two fewer sentences of code!!!


Two commonly used data transfer methods in struts 2 are as follows:

  1. Attribute Matching

  2. ModelDriven interface matching (often used for custom types)

Individuals prefer to use the first one. Why? Because _________________


Next, please Code Jun come on!!!

Attribute Matching

    Attribute matching can be divided into two cases, one is Java basic data type, the other is custom type. Look at Code below.

    1.1) Customize a Singer class with three attributes (singerID,singerName,area), code as follows

public class Singer {
	private int singerID;		//Singer ID
	private String singerName;	//Singer's Name
	private String area;		//Areas to which they belong
	public int getSingerID() {
		return singerID;
	}
	public void setSingerID(int singerID) {
		this.singerID = singerID;
	}
	public String getSingerName() {
		return singerName;
	}
	public void setSingerName(String singerName) {
		this.singerName = singerName;
	}
	public String getArea() {
		return area;
	}
	public void setArea(String area) {
		this.area = area;
	}
}

    1.2) Add two attributes (msg and singer) to the Action class defined earlier and modify the add method

public class Hello extends ActionSupport {
	private String msg;       //Basic types
	private Singer singer;    //Custom Types
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Singer getSinger() {
		return singer;
	}
	public void setSinger(Singer singer) {
		this.singer = singer;
	}

	// Default call
	@Override
	public String execute() throws Exception {
		System.out.println("Default call method!");
		return SUCCESS;
	}
	// Add to
	public String add() {
		System.out.println("Called the added method!");
		System.out.println("No."+singer.getSingerID());
		System.out.println("Full name:"+singer.getSingerName());
		System.out.println("Areas:"+singer.getArea());
		System.out.println("Leaving a message."+msg);
		
		return "add";
	}
}

    1.3) Write a JSP page for adding singers (singer_add.jsp above), and pay attention to the difference between the basic type and the custom type in the name attribute of the form element in the code (singer is the singer attribute in the Action above).

    <body>
  	<!-- Submitted to the wildcard mode of the previous configuration Action -->
  	<form action="smng_add.action" method="post">
  		No.<input type="text" name="singer.singerID" value="9527" /><br />
  		Full name:<input type="text" name="singer.singerName" value="Brother pony" /><br />
  		Areas:<select name="singer.area">
  			<option>Mainland</option>
  			<option selected="selected">Hong Kong</option>
  			<option>Taiwan</option>
  			</select><br />
  		Leaving a message.<input type="text" name="msg" value="How cool!!" /><br />
  		<input type="submit" value="Submission" />
  	</form>
    </body>

    1.4) Because the JSP page is placed under WEB-INF and cannot be accessed directly, an Access Action is configured in struts.xml.

<action name="singeradd" class="">
    <result>/WEB-INF/jsp/singer_add.jsp</result>
</action>

    1.5) Then you can visit http://localhost:8080/strDemo/singeradd.action to click submit to see the effect, is it simple!!!?


2. ModelDriven Interface

    The biggest difference between this method and attribute method is that the custom type is not encapsulated, but automatically assigned by implementing the interface. See the following code. (Action class is different, and JSP page name attribute does not need to add object name)

    2.1) Modify the Action class

public class Hello extends ActionSupport implements ModelDriven<Singer> {
	
	private String msg;
	
	//Do not encapsulate get and set, but must be instantiated, otherwise null pointer exception
	private Singer singer = new Singer();	
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	// Default call
	@Override
	public String execute() throws Exception {
		System.out.println("Default call method!");
		return SUCCESS;
	}
	// Add to
	public String add() {
		System.out.println("Called the added method!");
		System.out.println("No."+singer.getSingerID());
		System.out.println("Full name:"+singer.getSingerName());
		System.out.println("Areas:"+singer.getArea());
		System.out.println("Leaving a message."+msg);
		
		return "add";
	}
	//Here's the new code after the interface is implemented
	@Override
	public Singer getModel() {
		// TODO Auto-generated method stub
		return singer;
	}
	public void setModel(Singer singer){
		this.singer = singer;
	}
	
}

    2.2) Modify the JSP page (note that the name value of the form element is compared with the previous JSP page, where the attribute name of the singer object is written directly)

    <body>
  	<!-- Submitted to the wildcard mode of the previous configuration Action -->
  	<form action="smng_add.action" method="post">
  		No.<input type="text" name="singerID" value="9527" /><br />
  		Full name:<input type="text" name="singerName" value="Brother pony" /><br />
  		Areas:<select name="area">
  			<option>Mainland</option>
  			<option selected="selected">Hong Kong</option>
  			<option>Taiwan</option>
  			</select><br />
  		Leaving a message.<input type="text" name="msg" value="How cool!!" /><br />
  		<input type="submit" value="Submission" />
  	</form>
    </body>

OK, the final effect is exactly the same. That's all for today's content. Thank you, officers.

These entry-level things should not have appeared to give up the guest officer, if you can see it, please point out a compliment.


Keywords: Java JSP Attribute Struts

Added by bwcc on Mon, 10 Jun 2019 00:17:31 +0300