Deploying Struts2 in Eclipse

Source: Internet
Author: User

STRUTS2 is an excellent framework based on the MVC design pattern, developed by Struts1 and WebWork, with stable performance and is now a project of the Apache Software Foundation, to configure the development of STRUTS2 for initialization

STRUTS2 environment requirements are not high, JDK version 1.5 or more, Servletapi above 2.5, Tomcat version of more than 6.0 can, of course, it is recommended to use the latest version

First go to Apache official website to download Struts2 class library, http://www.apache.org the bottom of the Struts project hyperlink http://struts.apache.org, into the Struts homepage as follows:

  

Click the Download button to enter the download screen,

  

Currently struts latest version is 2.3.24.1, divided into Apps project case, Lib core driver, Docs document and SRC Framework source code, download according to need, I download here Struts-2.3.24.1-all.zip This includes all the packages, download to local extract out can, this page There are also previous versions of the download link, also including the Struts1 download link

If you want to download all the history packs, the link is: http://people.apache.org/builds/struts/

Then open eclipse for Java EE, create a new Dynamic Web site project

  

Enter the project name to confirm, and then build a normal Web project, and no difference before, also note to create the Web. XML configuration file

  

Then, open the Struts2 we just downloaded in the Lib directory, which has a lot of struts class library, is not completely necessary, generally we only import common, and other to the need to import, the need for the jar package such as:

  

After copying to the Lib directory, the Eclipse Project automatically imports the Web's class library

Then, configure Web. XML to add the following code between the <web-app> nodes, which is the filter for the STRUTS2 frame

1     <filter>2         <filter-name>struts2</filter-name>3         <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>4     </filter >5     6     <filter-mapping>7         <filter-name>struts2</filter-name>8         < Url-pattern>/*</url-pattern>9     </filter-mapping>

Configuring the filter is the most basic prerequisite for Struts2 to run, and it works when the Tomcat container starts up, The Web. XML is loaded first, so the filter defined in it is loaded, so the Strutsprepareandexecutefilter filter filters all requests under the project, and then uses the filter to capture the action request and then calls the appropriate method to process it, which is the general work of STRUTS2 Case, the Strutsprepareandexecutefilter class appears to allow developers to customize the filter to process user requests between request arrival processing

After configuring Web. XML, we need to establish the core configuration file of Struts2, the location is the SRC directory under the project, establish the Struts.xml file, he is responsible for configuring all the requests corresponding to various classes or methods, so is the core configuration file, the basic structure is as follows:

1 <?xml version= "1.0" encoding= "UTF-8"? >2 <! DOCTYPE struts PUBLIC3     "-//apache software foundation//dtd struts Configuration 2.0//en" 4     "/http Struts.apache.org/dtds/struts-2.3.dtd ">5 <struts>6 7 8 </struts>

The header is declared and all of our configurations are written in the inside of the <struts></struts> tag, so write one of the simplest configurations:

1     <package name= "Default" namespace= "/" extends= "Struts-default" >2         <action name= "Hellostruts2" class= "Net.zengzhiying.action.HelloStruts2Action" >3             <result>/result.jsp</result>4         </ Action>5         6         <action name= "Login" method= "login" class= "Net.zengzhiying.action.HelloStruts2Action" >7             <result>/login.jsp</result>8         </action>9     </package>

Here, an action is responsible for processing a request, packge we use the default package, when there are multiple packge, name must be separated, namespace is the space to access, our project name is HelloStruts2, if the namespace is/ Test should then be accessed, http://localhost:8080/HelloStruts2/test/the custom action

The first action corresponding to the access path is http://localhost:8080/HelloStruts2/hellostruts2.action of course, the default can also not add action

The second action corresponds to a http://localhost:8080/HelloStruts2/login.action

Then go to create an action class, that is Hellostruts2action, the class name must be named after the action of the hump, and inherit from the Actionsupport class, the code is as follows:

1 package net.zengzhiying.action; 2  3 import com.opensymphony.xwork2.ActionSupport; 4  5 public class Hellostruts2action extends Actionsupport {6< C3/>7     private static final long serialversionuid = 1L; 8  9     @Override10 public     String execute () throws Exc eption {         System.out.println ("Execute action"),         return success;14     }15     //New custom method 17 Public     String Login () {         return success;19     }20     21}

If the previous configuration file does not specify the method property, the default access is the Execute method, we also customize a login method, the success constant returns the default <result> specified view, the following set up the corresponding JSP file

result.jsp

1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2     pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 

In addition, the establishment of login.jsp is the same, not listed, now a basic STRUTS2 application is built, and now start the server, access to see the results:

  

And then I looked at the console, and it was printed out.

  

Simultaneous access to login is the same effect

  

The above is the basic configuration, in addition to the configuration file Struts.xml can also configure the exclamation point, wildcards and other flexible access, you can also configure the access suffix and other parameters, while the STRUTS2 framework powerful features more than these, but also need to continue to study

Deploying Struts2 in Eclipse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.