Preliminary use of struts

Environment construction:

1... / jar package configuration for pom.xml

2.web.xml configuration

3. Configuration of struts. XML
Struts. XML (core configuration file)

Configuration of struts-base.xml

Configure the web.xml file
(Note: Ctrl+Shift+t is a shortcut to find jar packages (find classes)

Invocation of dynamic methods

1. Write out the required method in the sub-controller

Configure struts-sy.xml

<!-- Relative MVC Differences
package: Used to classify a class of controllers
http://The "/sy" in localhost:8080/struts/sy/user_list.action corresponds to namespace="/sy" 
extends="base"Represents package inheritance
"/user_*"Medium  * It's in the address bar.   list     -->



<package name="sy" extends="base" namespace="/sy">
	<action name="/user_*" class="com.hsl.one.web.UserAction" method="{1}" >
	<result name="success">/test.jsp</result>//jsp page corresponding to display
	</action>
	</package>


Test: Page Address Bar

http://localhost:8080/struts/sy/user_list.action to view all methods
http://Method of adding localhost:8080/struts/sy/user_add.action
http://localhost:8080/struts/sy/user_delete.action deletion method
http://localhost:8080/struts/sy/user_edit.action

The test was successful:

There are three ways for jsp to transfer parameters to the background:

1.set Reference
2. Implementing modalDriven interface parameter transfer
3. Class instances. Attribute reference
Subcontroller:


Strts-sy.xml configuration adds:

<action name="/dome_*" class="com.hsl.one.web.DomeAction" method="{1}" >
	<result name="rs">/rs.jsp</result>
	</action>

jsp page reference:

//set parametric transmission
<a href="${pageContext.request.contextPath }/sy/dome_test1.action?sex=female">Test 1</a>

//  Implementing modalDriven Interface Reference Transfer
<a href="${pageContext.request.contextPath }/sy/dome_test2.action?uname=qw&upwd=1234">Test 2</a>

//  Class instances. Attribute referencing
<a href="${pageContext.request.contextPath }/sy/dome_test3.action?user2.uname=we&user2.upwd=12345">Test 3</a>

The results showed that:

Attachment: User entity class:

public class User {
	private String uname;
	private String upwd;
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpwd() {
		return upwd;
	}
	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}
	public User(String uname, String upwd) {
		super();
		this.uname = uname;
		this.upwd = upwd;
	}
	public User() {
		super();
	}
	@Override
	public String toString() {
		return "User [uname=" + uname + ", upwd=" + upwd + "]";
	}
	
}

The way in which background values are passed to jsp pages (interaction between struts and tomcat containers)

1. Injection
Coupling (commonly used)

Method of sub-controller:
public String dome() {
		HttpServletRequest request=ServletActionContext.getRequest();
		request.setAttribute("rs", "non-injection coupling mode");
		return "rs";
	}

Decoupling (using full path names, not often used)

public String dome() {
		ActionContext context=ActionContext.getContext();
		HttpServletRequest request=(HttpServletRequest)context.get("request Fully qualified path name");
		request.setAttribute("rs", "Injection decoupling method");
		return "rs";
	}

2. Non-injection
Coupling (commonly used)
Need to implement ServletRequestAware interface

Add an attribute:
private HttpServletRequest request;
Rewrite setServletRequest Method:

public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}
	//Test methods:
	
	public String dome() {
		request.setAttribute("rs", "Injection Coupling Mode");
		return "rs";
	}

Decoupling (using full path names, not often used)
Request Aware Interface to be Implemented

Rewrite setRequest Method:
public void setRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=(HttpServletRequest)context.get("request Fully qualified path name");
	}
//Test methods:
public String dome() {
		request.setAttribute("rs", "Non-injection decoupling method");
		return "rs";
	}

The above is to get the request object

  • 1. Reference can be passed using request object
  • 2. struts'unique way of passing parameters is called value stack passing parameters.
    In other words, as long as the action has a get method, it can get parameter values directly in the result set.

Strts-sy.xml configuration:

<action name="/tomcat_*" class="com.hsl.one.web.TomcatAction" method="{1}" >
	<result name="rs">/rs.jsp</result>
	</action>

jsp page effect test:

<a href="${pageContext.request.contextPath }/sy/tomcat_dome.action?sex=male">Test 1</a><br>
<a href="${pageContext.request.contextPath }/sy/tomcat_dome.action?uname=qw&upwd=1234">Test 2</a><br>
<a href="${pageContext.request.contextPath }/sy/tomcat_dome.action?user2.uname=we&user2.upwd=12345">Test 3</a>

The test results show that:
Test one by one

This result is validated.
"As long as the action has a get method, it can get parameter values directly in the result set."

Tags: Struts xml JSP Attribute

Posted on Wed, 21 Aug 2019 20:24:45 -0700 by pauleth