Some people may think that struts is not easy to learn. It seems that some concepts in it confuse people who have never been exposed to it. MVC1, MVC2, patterns... I wrote this article to let people who have never been exposed to struts have a simple introduction. Guidance, of course, is necessary to study struts systematically. There are a lot of fascinating things in it, but that is a story for another day.
This case includes homepage, user login, and website wizard pages. It's that simple. There is no profound concept of Struts. It mainly relies on hands-on experience and understanding with heart.
WEB Server uses tomcat4. Go to http://jakarta.apache.org to download struts1.1, release the zip file to c:struts, copy C:strutswebappsstruts-example.war to c:tomcat4webapps, and start tomcat. The war package is released into the struts-example folder, delete the war package, and rename the struts-example folder to test.
1. Change WEB-INFweb.xml to:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" " http://java.sun.com/j2ee/dtds/web-app_2_2.dtd ">
<web -app>
<!—This is the Controller in struts. The ActionServlet class is responsible for the system's command transfer. It reads configuration information from struts-config.xml and automatically starts a thread in the server background. If there are no special requirements (such as adding language editing functions), programmers can ignore this part and just use it. -->
<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>
<!--The servlet of this system can be mapped to a file with the suffix cool, instead of the common .jspdo, etc. The suffix name can be changed to any name, of course the name must be healthy #◎¥%! -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.cool</url-pattern>
</servlet-mapping>
<!--The default home page of this system is index.jsp, there can be multiple, the system will find it in order, similar to IIS-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2. Change testWEB-INF struts-config.xml to:
<?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>
<!--FormBean is a concept of struts. It is essentially a JavaBean. It is used to automatically store the values of each field in the page form and backfill the form fields at the appropriate time. There is no need to request.getParameter ("fieldName") like the traditional; , often used by actions in action-mappings -->
<form-beans>
<!—We will add a UserForm class later to store user information. -->
<form-bean name="userForm" type="test.UserForm"/>
</form-beans>
<!--This stores the global forward address that can be used by the entire system, similar to window.location('index.jsp'); in JavaScript, and also similar to various buttons on the TV controller. Channel switching, color adjustment, etc. are the control flow of web applications based on Struts. Under normal circumstances, after an Action is processed, it will be forwarded to a JSP page for display. This is also the key point of the implementation of MVC in JSP. -->
<global-forwards>
<!--failed.cool will be treated as a servlet request, and the corresponding action processing will be found in action-mappings. -->
<forward name="failed" path="/failed.cool"/>
<forward name="regist" path="/regist.jsp"/>
</global-forwards>
<!--Remember the request with the suffix cool in web.xml? They are handled here. This is equivalent to the Model part of struts. The Model part is a more flexible place in struts. -->
<action-mappings>
<!--To process the request of register.cools, the FormBean used is userForm, which is the test.UserForm class. When an error occurs during the processing, index.jsp will be returned-->
<action path="/regist" type="test.RegistAction" name="userForm" scope="request" input="/index.jsp" />
<action path="/overview" forward="/hello.jsp"/>
<action path="/failed" forward="/wuwu.jsp" />
</action-mappings>
</struts-config>
3. Add a FormBean with the class path 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;}
}
4. Add a subclass of Action, the class path is test. RegistAction, the following is the content of 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)
throwsException
{
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
UserForm userform = (UserForm) form;
//Here you can call other classes to perform database writing or other logical judgments //If the value of the parameter name passed by UserForm is the default lpw, it will forward to failed.
// This name will be searched for the mapped url address in <global-forwards> of struts-config.xml // (it can be an absolute path or a relative path). For this example, it goes to failed.cool.
//Remember? All requests with the suffix "cool" go to action-mappings to find // the corresponding action processing. The final directory is wuwu.jsp*/
if( "lpw".equals(userform.getName()) )
return (mapping.findForward("failed"));
else
return (mapping.findForward("regist"));
}
}
5. All the following new or modified pages are equivalent to the View part of struts. Change the homepage index.jsp to:
<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ page import = "test.*" %>
<a href="overview.cool">Site Navigation</a><br>
<form action="regist.cool" method="post">
<!—The name of the field in the form must be the same as the parameter in the UserForm, so that the data can be automatically obtained without using request.getParameter("param");-->
User:<input type="text" name="name"><br>
Password:<input type="password" name="ps"><br>
<input type=submit value="Add user">
</form>
6. Add hello.jsp for site navigation:
<h1>site map</h1>The following is content filling by reader
7. Add wuwu.jsp. When no new user logs in, they will be redirected to this Page:
<%@ page contentType="text/html;charset=GBK" language="java" %>
<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
Existing users: <%=beanlpw.getName()%><br>
Password: <%=beanlpw.getPs()%><br>
No new user was obtained!
8. Add register.jsp. When a new user logs in, he will be redirected to this page:
<%@ page contentType="text/html;charset=GBK" language="java" %>
<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
New user account: <%=beanlpw.getName()%><br>
Password: <%=beanlpw.getPs()%>
9. Start tomcat4, type http://localhost:8080/test/index.jsp in the browser, operate it, you can see the results, and initially understand the M of struts , V, and C parts work together. Of course, this is the author’s good intention. If readers are confused, please point out the error:)