Detailed steps for the first program under Mac Struts2 (with source link)

Source: Internet
Author: User

Mac Struts2 the first program in detail Step 1. Create a new Web project (1) Open MyEclipse 2015, select the File->new->web porject menu, and create a new Web project.
(2) Configure project information to fill in the project name Myfirststrust2webproject, select the default project storage path, selected in Java EE 6 version.                            Select Next, then next, choose Default to create default home page index.jsp and Web. XML (Web. xml file) files, these two files can also be added later, and then select Done. 2. Download the STRUTS2 framework to the STRUTS2 download link, select the all link, download the struts2 file package. Unzip the file,                 Get the struts-2.3.24 folder, under the Apps folder (some sample programs that hold Struts2), the Docs folder (which holds the Struts2 reference document), the Lib folder (the jar package that holds the Struts2), and the SRC folder (the source code that holds the STRUTS2). Unzip the file, get the struts-2.3.24 folder, under the Apps folder (some sample programs for storing Struts2), the Docs folder (the reference document for storing Struts2), the Lib folder (store Struts2 jar package), the SRC folder (store s Truts2 source code).
Lib folder holds all the jar packages for Struts2, we just need to pick out the 11 jar packages that must be used.



3. Introduction of the STRUTS2 framework into the project put the 11 jar packages selected in the previous step under the project Lib file. In MyEclipse Explorer, select Project, right-click, build path->configure Build Path






4. Configure the first STRUTS2 Framework Web program



1 Adding the Model classAddMystruts2model class, the code is as follows:
   
Package Myfirststrust2webproject.model;public class Mystruts2model {private String message;public Mystruts2model () { Super (); this.message = "This my first Struts2webproject";} Public String GetMessage () {return message;} public void Setmessage (String message) {this.message = message;}}




2 Adding an action classAddMystruts2action class, the code is as follows:

Package Myfirststrust2webproject.action;import Myfirststrust2webproject.model.mystruts2model;import Com.opensymphony.xwork2.actionsupport;public class Mystruts2action extends Actionsupport {     /** *  */private Static final Long Serialversionuid = 1l;private Mystruts2model mymodel;     Public String Execute () throws Exception {MyModel = new Mystruts2model (); return "Success";} Public Mystruts2model Getmymodel () {return mymodel;} public void Setmymodel (Mystruts2model mymodel) {This.mymodel = MyModel;}}

3 under the WebrootAddJSP file


Add myfirststruts2web.jsp with the following content:

<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "    pageencoding=" iso-8859-1 "%><%@ taglib prefix=" s "uri="/struts-tags "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

4. Add struts.xml In the unpacked folder found struts-2.3.24/apps/struts2-blank/web-inf/src/java/struts.xml , Copied to the project Struts2-blank under the SRC, and make changes, MyEclipse will automatically struts.xml to web-inf\classes under the deployment.
<pre name= "code" class= "HTML" ><?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><constant name=" Struts.devmode "value=" true "/><package name=" Basicstruts2 "  extends=" Struts-default "><action name=" myFirstStruts2 "class=" MyFirstStrust2WebProject.Action.myStruts2Action "method=" execute "><result name=" Success ">/ Myfirststruts2web.jsp</result></action></package></struts>


< constant name="Struts.devmode" value="true" />

Struts.devmode is the development mode of struts, the default value is False, change to true is to change the configuration in this file will not have to restart Tomcat
<action name= "<span style=" Color:rgb (57, 51, 255); Font-family:monaco; font-size:11px; " >myFirstStruts2</span> ">
struct.xml, find the corresponding class-I instantiation object, execute the corresponding execute () method
Execution Process:
Read XML---action is class-a class object found (each access must be a new object)
When class is not configured, the default class is Actionsupport.

Actionsupport Source

< span class= "keyword" style= "background-color:inherit; line-height:18px; Font-family:consolas, ' Courier New ', Courier,mono,serif; margin:0px; padding:0px; Border:none; Color:blue; Font-weight:bold ">     public  string execute ()   throws  exception { 

< Span style= "color:black; Background-color:inherit; line-height:18px; Font-family:consolas, ' Courier New ', Courier,mono,serif ">          return  success;

}

The most common is inherited from Actionsupport, the advantage is that you can directly use the Struts2 package good method.
5 adding Struts2 FilterLocate the project's "Web. xml" file, and then include the configuration information for the STRUTS2 filter in the Web. xml file:
<pre name= "code" class= "HTML" ><?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>myFirstStrust2WebProject</display-name> <welcome-file-list> <welcome-file> Index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp </welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</ welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> &L T;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>*.action</url-pattern> </ Filter-mapping></web-app>


by configuring this filter to start the Struts framework, the Strutsprepareandexecutefilter () method will read the default file Struts.xml under the Classpath to complete the initialization operation. Struts stores the contents of the Struts.xml file in memory in JavaBean form, eliminating the need to read the Struts.xml file repeatedly.
6. Rewrite index.jsp rewrite the index.jsp with the following:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

5 Run the program to Tomcat and run the view results. Project Source Link




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Detailed steps for the first program under Mac Struts2 (with source link)

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.