Struts development < configuring struts in Eclipse. A >

Source: Internet
Author: User

1. Get the Struts jar package

1.1 First download the struts package here at http://struts.apache.org/download.cgi#struts23163 (select Struts-2.3.16.3-all)

1.2 Unzip to get directories such as the following



Some official examples of struts under the apps directory

Docs has long been the official API documentation

LIB package is the full jar package of struts

SRC is a resource file for some examples



Note: Next we need to get the jar package we need, not all the jar files under the Lib folder, assuming all imports may conflict

So what are the jar packs we need?

1.3 Open the Apps directory, unzip Struts2-blank.war to get a demo sample file


1.4 Opening the jar package inside the web-inf/lib is the jar package that we need for basic struts operations. Take them out and stay with them.



2. Support for struts in the project

2.1 Open Eclipse New Dynamic web


2.2 Take the first step to get the jar package copied to the Project Web-inf/lib folder


2.3 Add Web. XML to the project and configure

Add the Web. xml file under the Web-inf root folder and configure the Struts filter

<span style= "FONT-SIZE:18PX;" ><?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 ">    <display-name>struts blank</ display-name>    <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>    <welcome-file-list>        <welcome-file> Index.html</welcome-file>    </welcome-file-list></web-app></span>

3. Build struts and achieve

3.1 New action inheritance Actionsupport in SCR

<span style= "FONT-SIZE:18PX;" >package Fzl.struts.demo;import Com.opensymphony.xwork2.actionsupport;public class UserAction extends Actionsupport {@Overridepublic String execute () throws Exception {System.out.println ("--------useraction-------"); Return "Success";}} </span>

3.2 Configuring the Struts.xml file

Create a struts.xml file under the SRC root folder and configure it

<span style= "FONT-SIZE:18PX;" ><?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= "Hello" class= "fzl.struts.demo.UserAction" ><result>/hello.jsp</result></action>    </package></struts></span>


4 Creating a display layer file

Build hello.jsp under the Web-inf directory

<span style= "FONT-SIZE:18PX;" ><%@ 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" >

Start Tomcat, enter the Http://localhost:port number in the Address bar/strutsdemo/hello you can get such as the following page


Here our struts configuration has been completed and implemented.

Finally, summarize


Basic steps:

1. Copy struts jar into project (these jar packages can be found in the blank project in apps)
2. Add struts2 filter to Web. xml
3. Configure STRUTS2 configuration file (create Struts.xml file in src folder)
4. Create ACTION (action is a Pojo class)
4.1. Write the Execute method for action
4.2. Configure the action and return result set in the Struts.xml file


Struts development &lt; configuring struts in Eclipse. A &gt;

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.