Analysis of Struts Simple Example (including code annotation and configuration file description)

zhaozj2021-02-16  57

First you have to configure your environment.

Configure the Tomcat Environment 1, to my machine (IP 192.1.168.7, the user is GUEST, the password is not), copy J2SDK-1_4_2_02-Windows-I586-P.exe, Jakarta-Tomcat-5.0. 14.zip, Jakarta-struts-1.1.zip three press. 2, first double click J2SDK-1_4_2_02-windows-i586-p.exe installation JDK, it is best to install it into an root directory. It is assumed here to be installed under D: /J2SDK1.4.2_02. 3, install Tomcat. Tomcat does not need to be installed, just extract JAKARTA-TOMCAT-5.0.14.zip to the hard disk with it. Suppose it is decompressed to D: / Tomcat. 4, configure environment variables. Right click on the "My Computer" icon, select "Advanced" child, click the "Environment Variable" button. In "System Variable", the following environment variables are new: Name key value Description Java_Home D: /J2SDK1.4.2_02 is your JDK installation directory classpath.;% Java_home% / lib5, unzip jakarta-struts-1.1.zip to Any directory, copy the Struts-Example.war file under /jakarta-struts-1.1/webapp directory to Tomcat / Tomcat / WebApps 6, enter the / tomcat / bin directory, click Startup.bat to start Tomcat. (Click Shutdown.bat after tomcat) 7, let Tomcat run in the background, open IE, enter http: // localhost: 8080 / can enter the home page of Tomcat in the address bar, enter http: // localhost: 8080 / Struts-Example can enter an example of Struts.

I wish you all a successful installation :)

Then let's take a look at the two most important files in the struts example, both of which are configuration files.

Struts-config.xml (I have noticed the situation of specific configuration)

Requirements for .do in. DO are processed here. Here is equivalent to the Struts's Model section, the Model section is a flexible place in Struts. ->

Web.xml (I have noticed the situation of specific configuration)

action *. do

index.jsp

/web-inf/app.tld /web-inf/app.tld ->

These two files are the basic configuration of our JDK Tomcat Struts, pay attention: You need to restart Tomcat after changing this configuration!

Save your .jsp file in / your directory /, their write you can write with HTML or FrongPage and other tools. For everyone, it is relatively simple, I will no longer have a book, the code is as follows: index.jsp

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.util. *, java.sql. *, java.text. *, java.io. *"%>

Enter the success will return to the "success" page, or return to the "fail" page

input:
success
Fail

Success.jsp

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.util. *, java.sql. *, java.text. *, java.io. *"%>

Success!

Error.jsp

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.util. *, java.sql. *, java.text. *, java.io. *"%>

Error Page!

In the / your folder / web-inf / class / beans we write classes that need to process messages. The specific code is as follows:

GetParameterform.Java

package beans; import org.apache.struts.action.ActionForm; public class GetParameterForm extends ActionForm {private String valu = "null"; public GetParameterForm () {} public void setValu (String s) {valu = s;} public String getValu () {Return Valu;}}

ShowAction.java

package beans; 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 *;. import org.apache.struts.util *;. public final class ShowAction 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 (); GetParameterForm userform = (GetParameterForm) form; if (. userform.getValu () equals ( "success")) { Return (Mapping.Findforward ("Success"));} else {system.out.println (userform.getvalu ()); return (mapping.findforward ("fail"));}}}

Finally you can run.

Fill in the http: // localhost: 8080 / your folders in the address in Tomcat

This is a simple example. I hope everyone can like it. I don't know if my explanation is correct. Please advise!

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

New Post(0)