We recall the inadequacy of the servlet that was felt during learning Javaweb (JSP + servlet programming):
1 Servllet Many, the code in Web. XML is a lot. In this way, maintenance is not convenient, not conducive to team cooperation;
The 21 servlet portals are only doget or Dopost methods, and if you need to define other methods, you must call them in the first two. This can lead to a messy code structure;
The 3 let class is highly coupled to the servlet container, with two parameter request in each method ,response. If the server does not start, there is no way to initialize the two parameters. This can cause trouble for unit tests.
4 If there is a lot of functionality in a method in a servlet , this time, it can cause the method to be more complex, so that it is not conducive to maintenance
In a servlet , if you want to get data from a form on a page, you'll write a lot of lines in the method
Struts2's History
1, Servelet
2, Struts1
1. Write action
2, wrote a central control of the servlet
3, Actionform and the content in the form on the page is consistent
3,webwork
1. Make action completely loosely coupled with the servlet Container
2.attribute-driven and model-driven get data from forms on the page
3, the use of the concept of interceptors to the servlet container to overcome the first 4 shortcomings
4,struts1+webwork=struts2
A first example
To write the steps:
1 Create a Web project
2 Copy Web. Xml from the Struts jar package into the above project
3 Create an ordinary Java class, and we are accustomed to write the last word of its name as action. such as Helloworldaction. It should provide a method that returns a value of string.
4 Create the struts.xml[name under the SRC folder must be this] and copy part of the code struts2.xml the struts-2.3.1.2\apps\struts2-blank\web-inf\classes directory to them. Rewrite as follows:
1<?xml version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE Struts public3"-//apache software foundation//dtd Struts Configuration 2.0//en"4"Http://struts.apache.org/dtds/struts-2.0.dtd" >5 6<struts>7< PackageName= "HelloWorld" namespace= "/"extends= "Struts-default" >8<action name= "helloworldaction" method= "greet"9 class= "Com.struts2.action.HelloWorldAction" >Ten<result name= "Index" >index.jsp</result> One</action> A</ Package> -</struts>
5 after deploying to Tomcat, turn on the server and access
http://localhost:8080/struts2_helloworld/helloworldaction.action
Struts QuickStart First article--struts related XML configuration mapping and explanation