First understand the relationship between struts and MVC in the traditional MVC pattern, all requests are given to the servlet before the servlet calls JavaBean and the results are presented to the JSP. Structure diagram below struts is an open source project for the Jakarta Project team in the Apache fund, primarily the MVC design pattern, with its own controller (Actionservlet) in struts, also provides a variety of commonly used page tag library to reduce the JSP page scriptlet code, struts in fact belongs to the traditional technology developed a new application mode, its operation is still the nature of JSP, Servlet, JavaBean and other technology applications, The architecture diagram for struts is as follows. The relationship between MVC and the various components of struts after understanding the above we began to learn to configure the Struts development environment struts belongs to a set of separate development packages, so first from the Apache Jakart project (http://jakarta.apache.org/ Download the latest Struts Development Kit (http://struts.apache.org/), I'm using the Struts 2.1 development package, (Struts-2.1.8.1-all.jar) will all of the LIB directories in the Struts development package * . jar packages are copied to the%tomcat_home%\lib directory, or you can import the struts package directly using the MyEclipse tool, which is the package directory and then configure the Web. xml file
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns: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>
Configure the contents of the Struts-config.xml file below for specific explanations
Development of the first Struts program
Web. XML 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><constantname= "Struts.devmode"value= "true" /> <!--specify that each request arrives and reload the resource file - <constantname= "Struts.i18n.reload"value= "true"/> <!--specify that the automatic reload after each configuration file change - <constantname= "Struts.configuration.xml.reload"value= "true"/> <!--To Configure the theme to simple - <constantname= "Struts.ui.theme"value= "simple"/> < Packagename= "Struts"extends= "Struts-default"namespace="/"> <Actionname= "Login"class= "Com.oumyye.action.LoginAction"> <resultname= "Success">/success.jsp</result> </Action> </ Package></Struts>
Loginaction.java
Packagecom.oumyye.action;Importjavax.servlet.http.HttpServletRequest;ImportOrg.apache.struts2.ServletActionContext;ImportCom.opensymphony.xwork2.ActionSupport;/** * * @authoreven my yes * action*/ Public classLoginactionextendsActionsupport {/** * */HttpServletRequest Request=servletactioncontext.getrequest (); Private Static Final LongSerialversionuid = 1L; @Override PublicString Execute ()throwsException {String name=request.getparameter ("user_name");//Get user_nameRequest.setattribute ("name", name);//Set Value returnSUCCESS; }}
JSP interface
index.jsp
<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Body> <formAction= "Login.action">Name:<inputtype= "text"name= "user_name"> <inputtype= "Submit"value= "Jump"> </form> </Body></HTML>
success.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' index.jsp ' starting page</title> </Head> <% Stringname=(String) Request.getattribute ("name"); %> <Body>Welcome<%=name%>Jump Success!! </Body></HTML>
How Struts Works
Struts is a concrete implementation of the MVC design pattern; The core class in struts is Actionform, Action, which needs to be configured in the Struts-config.xml file.
Introduction to the Java Framework article---struts