In the Javaweb development process, if only using servlet,jdbc,jsp for development, you can also follow the MVC pattern, at this time, the servlet is equivalent to the control layer, is responsible for processing the business logic of the controller, but also need to get and return data, Linked jumps and forwards are encapsulated. Then it looks rather bloated. (similar to the MVC in iOS development, if Viewcontroller is burdened with data processing, business processing and page jump logic, it becomes bloated, so the latter MVVM,VIP mode further refines the module division).
Struts Framework, a servlet-based framework developed by Apache, adheres to MVC's design patterns to further improve development efficiency.
If you leave the Struts framework and use the servlet for project development, after the project is started, each interface in each module is likely to be written as a servlet, Each servlet is created as a Servlet object at the start of the first access (which can be configured) when the server starts. The Struts framework optimizes the implementation of the servlet by reducing the servlet to one through a filter, distinguishing between different actions to differentiate between different business function interfaces.
Use of the STRUCTS2 framework:
1. Import the Struts2 jar package:
Struts2-core-xxx.jar Core Feature Pack
Xwork-core-xxx.jar Xword Core Pack
Commons-fileupload-xxx.jar File Upload Feature Pack
Commons-io-xxx.jar IO read/write support file Upload function
Ognl-xxx.jar OGNL Expression Support Package
Commons-langxxx.jar Lang's expansion
Freemarker-xxx.jar Tag Template Library
Javassist-xxx.jar byte code processing
Additional on-demand import
2, configuring Web. Xml
Mainly introduced filter, Strutsprepareandexecutefilter, Core filter
<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>
3, Develop action
Action is an action class, usually inherited Actionsupport
Action to perform a business process and execute a specific request.
Must return a string of type string
Method has no parameters.
Package Actions;import Java.io.printwriter;import Javax.servlet.http.httpservletresponse;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public class LoginAction Extends Actionsupport {/** * */private static final long serialversionuid = 1l;public String login () throws Exception {HttpServletResponse response = Servletactioncontext.getresponse (); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out = Response.getwriter (); Out.write ("Success"); Out.close (); return null;}}
If a specific string is returned, the client will show the result returned by the Struts.xml configured in the page, return null, and then proceed to the response write method, which can simply return the string, equivalent to the API interface
4, Configuration Struts.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public "-//apache software foundation//dtd struts Configuration 2.0//en" "/http Struts.apache.org/dtds/struts-2.0.dtd "><struts> <package name=" Actions "extends=" Struts-default " > <action name= "login" class= "actions. Loginaction "method=" Login "> <result name=" Success ">/index.jsp</result> </action> </package> </struts>
Name: interface URI to access
Class: The action class that executes, with the full package name
Specific function names processed by method:action
(+) The use of the Java Web struts2 framework