A few days ago has been playing Struts2, toss to toss, finally configured my first STRUTS2 project helloworld!
There is a little excitement in my heart:)
That's a little nonsense. Configuration steps
1. New Project
New > Dynamic Web Project
The project name is named Struts2-helloworld.
2. Importing the JAR Package
Unzip the Struts2-blank.war from the Apps folder in the Struts2 document downloaded from the official website and web-inf all the jar packages under > lib (as if it were 13, This is the most basic jar package) copy to Struts2-helloworld under WebContent > Web-inf > lib.
3. Add struts2 filter in Web. xml
Locate the Struts2-helloworld project under WebContent > Web-inf > Web. XML (if not in your project, it may be that the new project was not in generate Web. XML deployment Descriptor option), then edit, add the following code to the <web-app></web-app> tab
<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>
4. Add Struts.xml
Find Java Resources > src under Project Struts2-helloworld, then create a new Struts.xml file in this directory, which can refer to Struts.xml in the Struts2-blank project. The specific code is as follows:
<?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><PackageName= "Default"Namespace="/"Extends= "Struts-default" > <action name= "index" > <>/index.jsp </result> </ action> </package> </>
5. Add a JSP page
Create a new index.jsp under the WebContent directory under the Struts2-helloworld project. The specific code is as follows:
<%@ Page Language="Java"ContentType="text/html; Charset=iso-8859-1"Pageencoding="Iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd"><Html><Head><MetaHttp-equiv= "Content-type"Content= "text/html; Charset=iso-8859-1 "><title>insert title here </title> </head> << Span style= "color: #800000;" >body> <h1>hello world! </h1> </body> </< Span style= "color: #800000;" >html>
Then you can run the project, open the server, and open the browser input Http://localhost:8080/Struts2-HelloWorld/index