Implementation of struts2

1. Introduction to the project directory


Second, configure the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
 <!-- entry of struts2
 	org.apache.struts2.dispatcher.ng.filter
  -->
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	 <filter-name>struts</filter-name>
  	 <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>


3. Configure the action layer control (this method needs to inherit ActionSupport)

package com.gomai.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	private String username;
	private int pwd;
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getPwd() {
		return pwd;
	}

	public void setPwd(int pwd) {
		this.pwd = pwd;
	}


	public String saveUser(){
		System.out.println("Acquired username: " + username);
		System.out.println("Password obtained: " + pwd);
		return "ok";
	}
	
}

Fourth, configure the struts.xml file (the file should be placed in the src path)

<?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>
		<package name="default" namespace="/" extends="struts-default">
			<action name="save" class="com.gomai.action.UserAction" method="saveUser">
				<result name="ok">/add.jsp</result>
			</action>
		</package>
	</struts>
	

Guess you like

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