Introduction to Web layered architecture
1, web[fine granularity layered structure:
The fine-grained layering can be divided into the following 6 types:
1). Presentation layer: html/css/js/jsp
2). Control layer: servlet
3). Entity layer: JavaBean
4). Business Logic Layer: JavaBean
5). Persistence Layer: Jdbc/c3p0/dbutils
6). Data layer: Mysql/oracle
2, web[coarse] granular layered structure (emphasis):
It can be divided into 3 layers by coarse granularity:
1). Composed of the above 1, 2, 3) MVC
What is MVC?
M is model (models layer), V is view (presentation layer), C is controller (control layer).
MVC is used only for the presentation layer and not for other layers.
2). Business Layer
3). Persistence layer
second, Struts introduction
Struts 2, the star-level product of the Apache Foundation, provides a clear implementation of MVC, which includes a number of key components involved in the processing of requests, such as interceptors, OGNL expression languages, stacks, and so on.
Historical review:
2001, STRUTS1 Web Open source framework appears (ancestor)
2004, WebWork Web Open source framework appears (descendants)
2007, the STRUTS2 Web framework appears (descendants), STRUTS2 is not struts1 upgrade version is not webwork upgrade version, mostly from WebWork, a little half from struts1.
JSF (javaserverface) Web framework (descendant) to become the Java standard.
Personal Understanding:
A large servlet, that is, encapsulating the common operations of Javaweb, is an important part of the current SSH framework.
The web framework personally seems to be a variety of methods, programming ideas to encapsulate, and then carry out a variety of configuration can be very convenient to develop, see the framework of the source code to help understand Daniel's ideas.
Third, download struts2
Download the address: http://struts.apache.org/download.cgi
Full Distribution:struts-2.3.16-all.zip (65MB)
Four, struts2 hello World
The first step is to use Eclipse to create a new Maven WebApp project, as shown in the following illustration:
Click Next, select WebApp Project, and then next:
Click Next, and then click Next to fill in the form below.
Click Finish
Step Two:
Add the Trusted Jar packs, Struts2-core-2.3.16.jar, and Commons-io-1.3.2.jar to the Pom.xml as follows:
Copy Code code as follows:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>com.amos</groupId>
<artifactId>struts2</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>struts2 Maven webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>struts2</finalName>
</build>
</project>
Step Three: Configure Web.xml
This is the web.xml in the Apps/struts2-blank.war in the Struts-2.3.16-all.zip package that copy just downloaded.
Copy Code code as follows:
<! DOCTYPE Web-app Public
"-//sun Microsystems, INC.//DTD Web application 2.3//en"
"Http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>archetype Created Web application</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>
</web-app>
Fourth step: Helloworldaction.java
Here inherits from the Actionsupport class, only in the console output "Welcome to use struts2!" String.
Copy Code code as follows:
Package com.amos.web.action;
Import Com.opensymphony.xwork2.ActionSupport;
STRUTS2 's Controller
public class Helloworldaction extends actionsupport{
Private static final long serialversionuid = 6872366878758961847L;
Override the Execute () method
Public String Execute () throws Exception {
System.out.println ("Welcome to use struts2!");
return Super.execute ();
}
}
Step Fifth: Copy the Struts.xml in apps/struts2-blank.war,web-inf/classes/, and then configure the following:
Copy Code code 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>
<package name= "com.amos.web.action" namespace= "/" extends= "Struts-default" >
<action name= "helloworldaction" class= "com.amos.web.action.HelloWorldAction" method= "execute" > </action >
</package>
</struts>
Copy the Struts.xml to the resource directory as shown in the following illustration:
Sixth step: Start Tomcat, enter http://localhost:8080/struts2/HelloWorldAction view console output
Here the browser will complain because there is no response return, but here is simply configuration struts2, only for the validation console output, so can be ignored.
v. Other
There are several examples in apps to study:
Struts2-blank.war Struts2-portlet.war Struts2-showcase.war
Struts2-mailreader.war Struts2-rest-showcase.war
Here I only import the first one, Struts2-blank.war, the import method selects the war package for import--warfile-, and then the next step.