Instance learning struts
The choice of pure JSP or pure servlet design sites has its limitations. Struts is a powerful tool to link them together. Struts can be used to develop applications based on the MVC model. For more information about the MVC concept, see gof's design patterns-Basics of reusable object-oriented software.
What you need to do now is download, install, and configure the following tools. operations may vary depending on different versions. For details, refer to their documents:
- Tomcat 4.1.24
- Apache 2.0.43, W/mod_jk2 2.0.43
- Java 2 SDK Standard Edition 1.4.0
- Struts 1.1
- Eclipse 2.1.0
Struts is written in Java. Therefore, JDK 1.2 or later is required. If JDK 1.4 is used, XML Parser and JDBC 2.0 Optional package binary are included by default.
New project
In this routine, we need to develop a simple web application that allows users to log on and log off. For the sake of simplicity, data is set as constants rather than stored in the database. After all, Struts is used, not Java.
First, create a directory, for example, logonapp, in the app home directory configured for Tomcat. Create directory SRC and WEB-INF in logonapp, create directory classes and Lib in WEB-INF, copy struts from struts distribution. jar to the lib directory, and copy $ catalina_home/common/lib/servlets. jar to the lib directory. Copy all struts *. TLD from struts distribution to the WEB-INF directory.
Now open eclipse and you will see four views. Now we want to create a new project, click File> new project, open a window, select Java in the first pane, and select Java project in the second pane, click Next. Enter the project name (or logonapp), remove the check box of use default, browse to the logonapp directory, and click Next. A new window appears, click Add folder on the source tab, add $ app_base/src, enter $ app_base/WEB-INF/classes in default output folder, and click Finish. Click window-> open perspective-> resource to check whether the. project file automatically contains all jar files in the lib directory.
Your logonapp/WEB-INF/Web. xml should be as follows:
<?xml version="1.0"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"><web-app> <!-- Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- The Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib></web-app>
Struts configuration file logonapp/WEB-INF/struts-config.xml is as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><struts-config> <form-beans> <form-bean name="logonForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> </form-beans> <global-forwards> <forward name="success" path="/main.jsp"/> <forward name="logoff" path="/logoff.do"/> </global-forwards> <action-mappings> <action path="/logon" type="org.monotonous.struts.LogonAction" name="logonForm" scope="session" input="logon"> </action> <action path="/logoff" type="org.monotonous.struts.LogoffAction"> <forward name="success" path="/index.jsp"/> </action> </action-mappings> <controller> <!-- The "input" parameter on "action" elements is the name of a local or global "forward" rather than a module-relative path --> <set-property property="inputForward" value="true"/> </controller> <message-resources parameter="org.monotonous.struts.ApplicationResources"/></struts-config>
Create View
Now return to eclipse and create a new page index. jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
Main. jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
You may notice that both pages Use convenient internationalization features, which requires at least one default property file applicationresources. properties:
index.title=Struts Homepageprompt.username=Usernameprompt.password=Passwordindex.logon=Log onmain.title=Struts Main pagemain.logoff=Log offerror.password.mismatch=Invalid username and/or password.
Create a controller
Logonaction. Java:
package org.monotonous.struts;import java.util.Locale;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 org.apache.commons.beanutils.PropertyUtils;public final class LogonAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Locale locale = getLocale(request); MessageResources messages = getResources(request); // Validate the request parameters specified by the user ActionErrors errors = new ActionErrors(); String username = (String) PropertyUtils.getSimpleProperty(form, "username"); String password = (String) PropertyUtils.getSimpleProperty(form, "password"); if ((username != "foo") || (password != "bar")) errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password.mismatch")); // Report any errors we have discovered back to the original form if (!errors.isEmpty()) { saveErrors(request, errors); return (mapping.getInputForward()); } // Save our logged-in user in the session HttpSession session = request.getSession(); // Do something with session... // Remove the obsolete form bean if (mapping.getAttribute() != null) { if ("request".equals(mapping.getScope())) request.removeAttribute(mapping.getAttribute()); else session.removeAttribute(mapping.getAttribute()); } // Forward control to the specified success URI return (mapping.findForward("success")); }}
Logoffaction. Java:
package org.monotonous.struts;import java.util.Locale;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.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.util.MessageResources;public final class LogoffAction 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(); session.removeAttribute("userattrib"); session.invalidate(); // Forward control to the specified success URI return (mapping.findForward("success")); }}
Let's take a look at it in the browser, but you may have to consider some security measures for this application before opening the champagne. Let's talk about it next time.