First example of struts2

Example: struts2-blank.war


step:

1. Create a web project : Dynamic Web project

2. Import the jar package : copy it directly from the lib in the example project. . .

u struts2-core-2.3.1.1.jar: the core class library of the Struts 2 framework

u xwork-core-2.3.1.1.jar: Command mode framework, WebWork and Struts2 are based on xwork

u ognl-3.0.3.jar: ObjectGraph Navigation Language,

u struts2 framework reads and writes object properties through it

u freemarker-2.3.18.jar: The template of UI label of Struts 2 is written by FreeMarker

u commons-logging-1.1.x.jar: The log package produced by ASF, the Struts2 framework uses this log

u package to support logging for Log4J and JDK 1.4+.

u commons-fileupload-1.2.2.jar: file upload component, this file needs to be added after version 2.1.6

u commons-io-2.0.1.jar: jar package for file transfer

u commons-lang-2.5.jar: enhancements to the java.lang package

u javassist-3.11.0.GA.jar is an open source class library for analyzing, editing and creating Java bytecode


3. Write a jsp page : a new one under WebContext.

4. Write Action server-side processing logic

Action is the action class that handles request requests, similar to servlet

package cn.tx.action;

public class helloAction {

	public String hello(){
		return "success";
	}
}
 
 

5. Configure the framework: web.xml, struts.xml

The web.xml file is located in the web-info file

struts.xml file is located in src

web.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>


struts.xml code:

<?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="hello" extends="struts-default">
		<action name="hello"//Write the project name when visiting +hello class="cn.tx.action.helloAction" method="hello">
			<result name="success">success.jsp</result>
		</action>
	
	</package>

	
</struts>

Finish. carry out testing:

http://localhost:8080/struts2_01/hello

http://88kan.cn






Guess you like

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