Struts of SSH Framework

Posted by Kyori on Wed, 13 Oct 2021 16:22:35 +0200

Preface: last time, I shared Maven of SSH framework with you. Today, I share struts of SSH framework with you. Struts is very similar to the custom MVC shared with you earlier, but there are also differences. Next, I will share my knowledge with you

1, Clear objectives:

        1. struts configuration

        2. Dynamic calling method of struts

        3. Three ways to pass parameters in struts

        4. Integration of struts and tomcat (cooperation)

2, struts configuration

        1. Steps: (compare with custom mvc)

                      1. Create dependency -- > Import jar package (mvc imports jar package manually, and struts can write relevant code directly in Maven's pom.xml file)

                            After creating a new struts project, just like Maven, first change the version in the web to 3.1                   

                            Secondly, the java version is changed from 2.3 to 3.1, and the Dymanic version is changed from 1.5 to 1.8 (correct it here. After completing the version change, write the absolute path when filling in webapp, that is, src/main/webapp, rather than directly writing webapp)

 

                              After that, we will change a version of the jdk. Last time, we shared the manual transformation with you. Today, we share the code transformation. Just copy the following code into the pom.xml file in maven. Of course, before copying, write < plugins > < / plugins > and write the code in the middle.

	<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

		</plugins>

                              Finally, the integration of struts and Tomcat: write the following code in < dependencies > < / dependencies >, and wait patiently after writing (download the required jar package at this time)

<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.13</version>
		</dependency>
	</dependencies>

                      2. Make relevant configuration -- > just configure the filter (mvc should configure the central controller in web.xml, and struts should write the filter)

                          web.xml: (the class name in < filter class > < / filter class > is to click ctrl+shfit+t to search struts prepareandexecutefilter, and then copy the full path name of the class)

 <filter>
  <filter-name>struts</filter-name>
  <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>*.action</url-pattern>
  </filter-mapping>

                      3. Development: struts and user-defined mvc are the same. The sub controller should inherit Actionsupport

  3, Dynamic calling method of struts

          Compared with the method of custom mvc, the dynamic calling method of struts is more flexible; There are three struts configuration files (the three configuration files are the same as the configuration file of custom mvc, and the three configuration files complement each other), and there is only one custom mvc configuration file (web.xml)

1. Configuration file (the main configuration file struts-sy.xml):

struts is a dynamic method called. Of course, the writing method is different from that of user-defined mvc, as shown in the figure below. In the name attribute of action, the writing method is / demo1_ *, However, in the custom mvc, write / Demo1 directly, and class represents the class to enter.

The second difference:

The result node appears in struts-sy.xml, indicating the final result interface.

The third difference:

The method attribute also appears in the figure, {1} represents the first method to get the access path, and changing 1 is the corresponding  / demo1_* Back*

Note: there is a *, which naturally comes up with the writing method of two * / demo1 * *, The first * also represents the method, and the second method represents the result interface to jump to

There are two ways to write *:

2. Sub controller class: corresponding method

public String add() throws Exception {
		System.out.println("add method");
		return "bookEdit";
	}
	
	public String del() throws Exception {
		System.out.println("del method");
		return "bookEdit";
	}
	
	
	public String edit() throws Exception {
		System.out.println("edit method");
		return "bookEdit";
	}

3. bookEdit interface:  

4. Implementation class Demo1.jsp:

    <h3>struts Method call </h3> 
	<a href="${pageContext.request.contextPath }/sy/demo1_add.action">newly added</a><br>
		<a href="${pageContext.request.contextPath }/sy/demo1_del.action">delete</a>	<br>
		<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">modify</a><br>

5. Operation results:

Run successfully:  

 

4, There are three ways to pass parameters to struts:

        There are three ways:

                        1. Modeldriven parameters

                        2. set mode parameter transfer:

                        3. Pass parameters according to object attributes:

1. ModelDriven parameter passing: write the code to implement the modelDriven class in the sub control class:

Entity class:

package com.zking.one.entity;

public class User {
	private String uid;
	private String uname;

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public User() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "User [uid=" + uid + ", uname=" + uname + "]";
	}

	public User(String uid, String uname) {
		super();
		this.uid = uid;
		this.uname = uname;
	}

}

demo1.jsp:

 <h3>struts Parameter transmission mode </h3>
	    	<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">Modeldriven Transmission parameter</a><br>
		<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">adopt set Method transmission parameter</a>	<br>
		<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&user2.uname=ls">Through object properties</a><br>
	     

Demo1Action:

public String list() throws Exception {
		System.out.println("list method");
		System.out.println(user1);

  Operation effect:

  2. Parameter transmission in set mode: if the User does not have the property of sex, write a sex in the action alone to generate get and set methods

Demo1Action:

demo1:

		<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">adopt set Method transmission parameter</a>	<br>

  Operation effect:

3. Pass parameters according to the object attributes: define a user called user2 in Demo1Action and write the get and set methods of user1 and user2, but there is no new. At the same time, it can also run the effect:

Demo1Action:

demo1:

		<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&user2.uname=ls">Through object properties</a><br>

Operation results:

5: Integration (cooperation) of struts and tomcat the purpose of writing struts project today is to test:

        1. There are two ways:

                            1. Directly define ServletActionContext.getRequest() for integration

                            2. Implement servletrequestaware and servletresponseaware in Demo1Action class for integration.

        2. Directly define ServletActionContext.getRequest() for integration:

Demo1Action:

HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 20);

bookEdit.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
Successful interface....
${ age }

Operation results:

        3. Implement servletrequestaware and servletresponseaware in Demo1Action class for integration:

In Demo1Action:

public String list() throws Exception {
		System.out.println("list method");
		System.out.println(user1);
		System.out.println(sex);
		System.out.println(user2);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 20);
		req.setAttribute("name", "sjx");
		return "bookEdit";
	}

And override setServletRequest and setServletResponse methods:

Operation results:

Run successfully:

  Summary: compared with custom MVC, Struts framework is more convenient to use, which is shared today     In the knowledge, the second point is that the dynamic call method of struts is more novel. The parameter transfer mode of Struts framework is richer than that of custom MVC. From then on, we can see the strength of Struts framework. Today's knowledge is shared here. I hope it will help you. If you have any negligence, please give me more advice! ​​​​​​​

Topics: ssh Struts