Java framework --- entry to Struts, java framework --- struts
First, understand the relationship between Struts and MVC. In the traditional MVC mode, all requests must be handed over to the Servlet for processing. Then, the Servlet calls the JavaBean and submits the results to the JSP for display. The following figure shows the structure of Struts, which is an Open Source project of the Jakarta project team in the Apache fund. It mainly implements the MVC design mode and has its own controller (ActionServlet) in Struts ), at the same time, various common page tag libraries are provided to reduce the Scriptlet code in JSP pages. Struts is actually a new application mode developed in traditional technology, the essence of its operation is still the application of JSP, Servlet, JavaBean and other technologies. The architecture of Struts is as follows. The relationship between MVC and Struts components after understanding the above, we began to learn to configure Struts Development Environment Struts is a separate set of development kit, so first from the Apache Jakart Project (http://jakarta.apache.org /) download the latest Struts Development Kit (http://struts.apache.org/), I use Struts 2.1 Development Kit, (struts-2.1.8.1-all.jar) Will Struts development kit in the lib directory all *. copy the jar package to the % atat_home % \ lib directory. You can also use the myeclipse tool to directly import the Struts package, which is the package directory and then configure the web. xml file
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Struts1</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>*.action</url-pattern> </filter-mapping></web-app>
The content of the configuration struts-config.xml file is explained in detail below
Develop the first Struts Program
Web. xml as above
Struts-config.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name = "struts. devMode "value =" true "/> <! -- Specify that each request arrives and reload the resource file --> <constant name = "struts. i18n. reload" value = "true"/> <! -- Specifies that the configuration file is automatically reloaded after each change --> <constant name = "struts. configuration. xml. reload" value = "true"/> <! -- Configure the topic as simple --> <constant name = "struts. ui. theme "value =" simple "/> <package name =" Struts "extends =" struts-default "namespace ="/"> <action name =" login "class =" com. oumyye. action. loginAction "> <result name =" success ">/success. jsp </result> </action> </package> </struts>
LoginAction. java
Package com. oumyye. action; import javax. servlet. http. httpServletRequest; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionSupport;/***** @ author even my * action */public class LoginAction extends ActionSupport {/*****/HttpServletRequest request = ServletActionContext. getRequest (); private static final long serialVersionUID = 1L; @ Override public String execute () throws Exception {String name = request. getParameter ("user_name"); // get user_name request. setAttribute ("name", name); // set the return SUCCESS ;}}
Jsp Interface
Index. jsp
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Success. jsp
<% @ 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">
How Struts works
Struts is a specific implementation of MVC design pattern; the core class in Struts is ActionForm, Action, all need to be configured in the struts-config.xml file.