Struts fool learning (one day) (ZT)

xiaoxiao2021-03-06  19

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 by hand, then experience ~! @ # $% ^ & Web Server with 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:

action org.apache.struts.Action.ActionServlet config / web-inf / struts -config.xml 1 action *. Cool index.jsp

Second, change TEST / Web-INF / STRUTS-Config.xml: "http://jakarta.apache.org/struts/dtds /struts-config_1_1.dtd ">

3, add a formBean, class path is Test.Userform, the following is the content of this class :

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 an action subclass, class path is TEST. RegontAction, the following is This class's content:

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; // You can call other classes to execute database write or other logic judgment // If the usermm transmitted by the parameter Name of Name is the default LPW, will forward to failed, // The name will find a mapped URL address / / (which can be an absolute path, or a relative path), for this example, is transferred to failed.cool, // Remember ? The suffix is ​​a request for Cool to find // corresponding to // in action-mapings, the final directory is wuw.jsp * / if ("lpw" .Equals (userform.getname ()) Return (Mapping.Findforward ("failed ")); Else Return (" regist "));}} 5, all newly added or modified pages are equivalent to Struts's View section, change the home page index.jsp: <% @ page contenttype = "text / html; charset = GBK" language = "java"%> <% @ page import = "test. *"%> Site navigation User:

password:

6. Increase hello.jsp for site navigation:

Site Mapthe Following IS Content Filling by Reader

7. Increase wuwu.jsp, when there is no new user login, turn to this page: <% @ Page ContentType = "text / html; charset = GBK" Language = "java"%> 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, 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 :)

转载请注明原文地址:https://www.9cbs.com/read-40290.html

New Post(0)