Recently, I started learning struts. First, I read the book for a few days, and then I want to write a simple login program. However, during the write process, it is found that the request submitted on the page can be forwarded to the action you write. However, the execute method in the action is not executed, and it is not easy to tune it. Then I wrote a super simple program that only submitted operations, but still failed, depressing. The next day, I found the problem with others and finally solved it.
Let's talk about the environment first: myeclipse5.5 (the machine is too bad and the version is too high to run), javasdk1.6, and struts1.3.10.
Project Structure: page1.jsp, page2.jsp, and pageaction. java. Page1 submits the request to pageaction. Do. pageaction prints "success" on the console and returns a "success ". Jump to page2.jsp.
Original code:
Page1.jsp
========================================================== ===
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<Form method = "Post" Action = "pageaction. Do">
<Input type = "Submit" value = "Submit"/>
</Form>
========================================================== ===
Page2.jsp
========================================================== ===
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<%
Out. println ("this is page2.jsp ");
%>
========================================================== ===
Pageaction. Java
========================================================== ===
Package com. Action;
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import org. Apache. Struts. Action .*;
Public class pageaction extends action {
Public actionforward execute (actionmapping mapping, actionform form, servletrequest request,
Servletresponse response) throws exception {
System. Out. println ("success ");
Return Mapping. findforward ("success ");
}
}
========================================================== ====
Web. xml
========================================================== ====
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.5" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 3 </param-value>
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 3 </param-value>
</Init-param>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
</Web-app>
========================================================== =====
Struts-config.xml
========================================================== =====
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype Struts-config public
"-// Apache Software Foundation // DTD struts configuration 1.3 // en"
Http://struts.apache.org/dtds/struts-config_1_3.dtd>
<Struts-config>
<Action-mappings>
<Action Path = "/pageaction" type = "com. Action. pageaction">
<Forward name = "success" Path = "/page2.jsp"/>
</Action>
</Action-mappings>
</Struts-config>
========================================================== ======
At the beginning, a blank page is displayed after you click the button on page1, and no error is reported in the background, which is very depressing. No change is possible. Later
The Action source code finds that the action has overloaded the execute method. They are:
========================================================== ====================
Public actionforward execute (actionmapping mapping, actionform form,
Servletrequest request, servletresponse response)
Throws exception {
}
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
}
========================================================== ====================
Then, the program execution process is tracked. There is a method in the process:
Protected actionforward processactionperform (httpservletrequest request,
Httpservletresponse response, Action action, actionform form,
Actionmapping mapping)
Throws ioexception, servletexception {
Try {
Return (action.exe cute (mapping, form, request, response ));
} Catch (exception e ){
Return (processexception (request, response, E, form, mapping ));
}
}
The second execute above is called. Execute in your own pageaction to execute. Finally. I have seen many people on the Internet have this problem. I don't know if you are in the same situation as me.
To sum up, you still can't rush to learn. If you have any problems, you need to find the answer slowly. In this way, you can remember better. In addition, you need to follow certain steps to find errors. I wrote a blog for the first time, but I am not doing very well. I just pasted my code.