Perhaps some people think that Struts is not easy to learn, it seems that there are some concepts that have not been confused, MVC1, MVC2, model ... I wrote this article, I want to have someone who has never touched Struts, there is a simple entry Guide, of course, systematically learn Struts is necessary, there are a lot of people who are bothered in Dongdong, that is later. The case includes the home page, the user login, the website wizard page. It's so simple, there is no deep Struts concept, mainly relying on hand, then experience it with your heart. Web Server uses Tomcat4. To http://jakarta.apache.org download struts1.1, release the ZIP file to C: / Struts, copy c: /struts/webapps/struts-example.war to C: / Tomcat4 / WebApps, start Tomcat, The WAR package is released to the Struts-Example folder, remove the WAR package, and rename the Struts-Example folder Test. First, change Web-INF / Web.xml:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
Inc.//dtd Web Application 2.2 // en "
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
The system's instruction is transferred, and it is responsible for both the ActionServlet class.
It reads configuration information from Struts-Config.xml,
And start a thread in the server background.
If there is no special requirement (such as adding language editing function),
The programmer can use it for this part. ->
Action.ActionServlet servlet-clas>
init-param>
servlet>
Instead of common .jspdo, etc., the suffix name can be changed to any name,
Of course, the name is healthy # ◎ ¥%! ->
servlet-maping>
There are multiple, the system is looking for, similar to IIS ->
welcome-file-list>
web-app>
Second, change TEST / Web-INF / STRUTS-Config.xml:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation //
DTD Struts Configuration 1.1 // en "
"http://jakarta.apache.org/struts
/dtds/struts-config_1_1.dtd ">
Use the value of each domain from the stored page form and fill the form field when appropriate.
No need to be like the traditional REQUEST.GETPARAMETER ("FieldName");
Often used by action-mapings ->
form-beans>
Similar to Window.Location ('Index.jsp') in JavaScript;
It is also similar to various buttons on the TV controller, which can be converted.
Toning, etc. is a control process flow based on Struts.
Under normal circumstances, an Action is processed,
Will forward to a JSP page for display.
This is also the point of implementation of MVC in JSP. ->
Look for the corresponding Action processing in action-mapings. ->
global-forwards>
They are processed here. Here is equivalent to the Model section of Struts.
The Model section is a flexible place in Struts. ->
Formbean used is Userform, both Test.Userform class,
Returns index.jsp -> when the processing is wrong.
Name = "userform" scope = "request" infut = "/ index.jsp" /> action-mappings> Struts-Config> Third, add a FORMBEAN, class path is Test.Userform, the following is this content: Package test; import org.apache.struts.Action.actionform; Public Class Userform Extends ActionForm {private string name = "lpw"; //username PRIVATE STRING PS = "1111"; //password Public userform () {} Public void setname (String S) {Name = S;} Public string getName () {return name;} Public void setps (String S) {ps = s;} Public String getps () {Return PS;}} Fourth, add an action subclass, class path is Test. Registry, the following is this class: Package test; Import java.lang.reflect.InvocationTargeTexception; Import java.util.locale; Import javax.servlet.servletException; Import javax.servlet.http.httpservletRequest; Import javax.servlet.http.httpsession; Import javax.servlet.http.httpservletResponse; Import org.apache.struts.Action.action; Import org.apache.struts.Action.Actionerror; Import org.apache.struts.Action.Actionerro; Import org.apache.struts.Action.actionform; Import org.apache.struts.Action.actionForward; Import org.apache.struts.action.actionmapping; Import org.apache.struts.util.MessageResources; Import test.userform; Public Final Class RegontAction Extends Action { Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response Throws Exception { Locale locale = getLocale (request); MessageResources Messages = GetResources (Request); HttpSession session = request.getSession (); userform userform = (userform) form; // You can call other classes to execute database write or other logic judgments. / / If the value of the parameter name from UserForm is the default LPW, forward forward to failed / / This name will go to Struts-Config.xml / / (Can be an absolute path, or the relative path), For this example, it is to Failed.cool, // do you remember? Request for Cool for Cool to find-mapings in Action-mapings // Corresponding action, the final directory is wuwu.jsp * / IF ("LPW" .equals (userform.getname ())))) Return (Mapping.FindForward ("failed")); Else Return (Mapping.FindForward ("regist"); } } 5. All newly added or modified pages are equivalent to the Struts's View section, change the home page index.jsp: <% @ Page ContentType = "Text / HTML; Charset = GBK "Language =" Java "%> <% @ Page Import = "Test. *"%>
You can achieve automatic data automatic acquisition function,
No need to use Request.GetParameter ("param"); ->
User:
Password:
form>
6. Increase hello.jsp for site navigation:
IS Content Filling by Reader
7. Increase wuwu.jsp, when no new user logs in, will be transferred to this page:
<% @ Page ContentType = "Text / HTML;
Charset = GBK "Language =" Java "%>
Class = "Test.Userform" Scope = "session" /> Existing users: <% = beanlpw.getname ()%> Password: <% = beanlpw.getps ()%> Didn't get new users! 55555 ~~~~~ 8, increase regist.jsp, when there is a new user login, turn to this page: <% @ Page ContentType = "text / html; charset = GBK" Language = "java"%> New user account: <% = beanlpw.getname ()%> Password: <% = beanlpw.getps ()%> Nine, start Tomcat4, type http: // localhost: 8080 / test / index.jsp Operation, you can see the result, and initially understand the synergy of Struts M, V, C, of course this Is the good wish of the author, if the reader can see a mist, welcome to point out the mistake :)