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"?>
XML Version = "1.0" Encoding = "ISO-8859-1">
Package test; import org.apache.struts.action.actionform; public class userform extends actionform {private string name = "lpw"; // User name 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;}} four, add one Action's subclasses, class paths are Test. RegontAction, 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.ActionErrors; 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 RegistAction 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; // here be called Other classes to perform database write or other logical judgment // If you have received The value of Name is the default LPW, put the Forward to FAILED, // This name will look for the mapped URL address in struts-config.xml
<% @ page contenttype = "text / html; charset = GBK" language = "java"%> <% @ page import = "test. *"%> site navigation a >
7. Increase wuwu.jsp, when no new user logs in, will be transferred to this page:
<% @ page contenttype = "text / html; charset = GBK" language = "java"%>
Password: <% = beanlpw.getps ()%>
Didn't get new users!
8. Increase regist.jsp, when there is a new user login, turn to this page:
<% @ Page ContentType = "Text / HTML; Charset = GBK" Language = "Java"%>
Password: <% = beanlpw.getps ()%>
Nine, start Tomcat4, type http: // localhost: 8080 / test / index.jsp, you can see the result, and initially understand the synergy of the Struts M, V, and of course This is the good wish of the author. If the reader can see a mist, welcome to point out the mistake :)