A simple example of struts2 development without the IDE

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.