1. Create a Web project MyApp in Tomcat's webapp with the following file and path:
①/webapp/myapp/login.jsp
②/webapp/myapp/welcom.jsp
③/webapp/myapp/error.jsp
④/webapp/myapp/web-inf/web.xml
⑤/webapp/myapp/web-inf/classes
⑥/webapp/myapp/web-inf/classes/struts.xml
⑦/webapp/myapp/web-inf/classes/test/loginaction.class
⑧/webapp/myapp/web-inf/lib
2. Add the following jar files in Lib: commons-fileupload-1.3.1, commons-io-2.2, commons-lang3-3.2, commons-logging-1.1.3, freemarker-2.3.19, Javassist-3.11.0.ga, ognl-3.0.6, struts2-core-2.3.20, xwork-core-2.3.20 (depending on the version of the Struts2 you use)
3. Source Code
#login. jsp
---------------------------------------------------------------------------------
<% @page language= "java" contenttype= "Text/html;charset=utf-8"%>
<title> Landing Page </title>
<body>
<form action= "Login.action" method= "POST" >
<tablealign= "Center" >
<caption> Landing Page <tr>
<td>username<input type= "text" name= "username"/></td>
</tr>
<tr>
<td>password<input type= "text" name= "password"/></td>
</tr>
<tr>
<TD colspan= "2" ><inputtype= "submit" value= "submit"/><inputtype= "reset" value= "reset"/></td >
</tr>
</table>
</form>
</body>
#welcome. jsp
---------------------------------------------------------------------------------------
<% @page language= "java" contenttype= "Text/html;charset=utf-8"%>
<title> Success Page </title>
<body>
You have landed!
</body>
#error. jsp
--------------------------------------------------------------------------------
<% @page language= "java" contenttype= "Text/html;charset=utf-8"%>
<title> Failure page </title>
<body>
Login failed!
</body>
#LoginAction. java
-----------------------------------------------------------------------
{
return" Success ";
}
else
{
return "error";
}
}
}
4.web.xml Configuration
--------------------------------------------------------------------------------------
<?xmlversion= "1.0" encoding= "UTF-8"?>
<web-app id= "Webapp_9" version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "HTTP://JAVA.SUN.COM/XML/NS/J2EEhttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5.struts2.xml Configuration
--------------------------------------------------------------------------------------
<?xmlversion= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts public
-//apache software foundation//dtd struts configuration2.3//en
Span style= "font-size:14px;font-family: ' Microsoft Jas Black ', ' sans-serif ';" >HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-2.3.DTD
<struts>
<constantname= "Struts.enable.DynamicMethodInvocation" value= "false"/>
<constant name= "Struts.devmode" value= "true"/>
<package name= "Default" namespace= "/" extends= "Struts-default" >
<action name= "Login" class= "test. Loginaction "Method=" Execute >
<resultname= "Success" >/welcome.jsp</result>
<resultname= "Error" >/error.jsp</result>
</action>
</package>
</struts>
6. Frequently Asked questions
1 ) Input http://localhost:8080/myapp/login.jsp tip: Httpstatus 404-/myapp/login.jsp
The main reasons may be: The jar file in ①lib is not enough
The package and action configuration in ②struts.xml is incorrect
2 ) login.jsp Page Click submit to prompt for null pointer error
The main reasons may be: ①loginaction No package name, directly stored in the root directory of the classes
This article from "7121961" blog, declined reprint!
A simple example of struts2 development without the IDE