SSH framework-Struts in-depth explanation (1)

Source: Internet
Author: User

I learned struts, but for its origins, and why to use action and struts. the XML method does not use the previous servlet method. I have some questions about the disadvantages of the previous method. What convenience does struts bring to us?

The following are my answers!

 

Struts:

 

With JSP and Servlet technologies widely used in Web-based applications, Java developers have proposed some better development modes to improve web application maintainability and reuse. The two common JSP application architectures are model1 and Model 2. For details, see (Java learning-Comparison of Two Java programming modes)

 

Two models are briefly reviewed:

 

Mode1 is a mode centered on JSP files. jsp is not only responsible for expressing logic, but also for controlling logic. Logical coupling in the page. This processing method can be used for some small projects. However, when developing large projects, it is difficult for the page to grasp the flow direction and the relationship between interfaces is too strong, it makes it difficult to modify and maintain programs. In addition, the intersection of Program Logic and page display is neither convenient for division of labor or code reuse. Such programs are not robust and scalable.


In Model 2, the servlet acts as the front-end controller. client requests are not directly sent to JSP, but to servlet. Then, the servlet calls different transaction logic based on specific requests, return the processing result to the appropriate page. The most important thing is that model2 splits the business logic from the JSP file. After the separation, the JSP file is simply displayed, which is often referred to as the view; the independent transaction logic and data processing are often referred to as the model, and the controller control itself is the MVC mode.

The MVC mode provides great convenience for the development and maintenance of large programs. However, the advantages of model2 also lead to its shortcomings. Due to decoupling and layering, the compilation of model2 becomes complicated. Especially for large projects, there are too many Servlets, frequent redirection, and procedures, configuration is not easy to centrally manage.

 

Therefore, based on the above reasons, Struts extracts some functions and then encapsulates them so that we can better use them. The above is too general and may be hard to imagine. The following describes the disadvantages of not using struts with specific examples!

 

Specific Example Analysis (without struts)

 

Add.html

<HTML> <body> <form action = ".. /addgrade "method =" Post "> Student ID: <input type =" text "name =" userid "> <br> Name: <input type = "text" name = "username"> <br> language: <input type = "text" name = "Chinese"> <br> mathematics: <input type = "text" name = "maths"> <br> English: <input type = "text" name = "English"> <br> <input type = "Submit" value = "save"> <input type = "reset" value = "cancel "> </form> </body> 

Configuration File

<servlet><servlet-name>addgradeservlet</servlet-name><servlet-class>servlet.addGradeServlet</servlet-class></servlet><servlet-mapping><servlet-name>addgradeservlet</servlet-name><url-pattern>/addGrade</url-pattern></servlet-mapping>

Addgradeservlet. Java: Servlet

public class addGradeServlet extends HttpServlet{public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String strUserId = request.getParameter("USERID");String strUserName = request.getParameter("USERNAME");String strChinese = request.getParameter("CHINESE");String strMaths = request.getParameter("MATHS");String strEnglish = request.getParameter("ENGLISH");Grade grade=new Grade();grade.setUSERID(strUserId);grade.setUSERNAME(strUserName);grade.setCHINESE(strChinese);grade.setMATHS(strMaths);grade.setENGLISH(strEnglish);gradeDao gradedao=new gradeDao();gradedao.insertGrade(grade); ArrayList<Grade> gradeList=(ArrayList)gradedao.listGrade();HttpSession session=request.getSession();ServletContext scx=session.getServletContext();scx.setAttribute("gradeList",gradeList);response.sendRedirect("javabean_test/show.jsp");}public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{this.doPost(request, response);}

Analysis: the function of this code segment is to get the student ID, name, language, mathematics, English and other information, perform corresponding operations, and turn to the corresponding page.


Question 1: For a jump page, it is the page name in the project, for example, show. JSP and so on are all written to death. For example, if you want to change a page, because the file names have been written to the program, it is very troublesome to change and does not comply with the OCP principle.

In addition, it is not difficult to find that the jump code of each method is almost the same, and only the specific page of the jump is different, then we can extract the content that does not need to be modified, to make the framework, you need to make some changes. You only need to configure the JSP name.

 

Question 2: In the above addition function, we need to obtain multiple parameters from the foreground and then set them one by one to the object, which not only increases the amount of code, it also makes our program difficult to maintain. The Code is as follows:

String strUserId = request.getParameter("USERID");String strUserName = request.getParameter("USERNAME");String strChinese = request.getParameter("CHINESE");String strMaths = request.getParameter("MATHS");String strEnglish = request.getParameter("ENGLISH");Grade grade=new Grade();grade.setUSERID(strUserId);grade.setUSERNAME(strUserName);grade.setCHINESE(strChinese);grade.setMATHS(strMaths);grade.setENGLISH(strEnglish);

Question 3: The addservlet is used to add and delete, modify, and query functions. For a function block, at least four servlets are required for management, increased the burden on the program, making it difficult to maintain. Even if we manage all the servlets of a function block in a unified manner, we need to add the if judgment statement, similar to the following code:

Protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {If (constants. show_add.equals (getcommand () {showadd (request, response);} else if (constants. add. equals (getcommand () {Add (request, response);} else if (constants. del. equals (getcommand () {del (request, response);} else if (constants. audit. equals (getcommand () {Audit (request, response) ;}else {// search by PAGE (request, response );}} /*** Delete * @ Param Request * @ Param response * @ throws servletexception * @ throws ioexception */private void del (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {string [] flowcardvounos = request. getparametervalues ("selectflag"); flowcardmanager. delflowcard (flowcardvounos); response. sendredirect (request. getcontextpath () + "/servlet/flowcard/flowcardservlet ");}

But what are the drawbacks of such writing? Why is the if statement unstable? There is a function for adding, deleting, querying, and submitting for review. Now I want to add and modify the function, so I need to modify the code-in violation of the OCP principle. Therefore, it has poor ability to adapt to requirements.

 

The above are the problems that struts can solve. In turn, let's make a statement about the problems in our program when we don't need struts. Of course, Struts not only solves these problems, the above is just to help you understand the simple examples. please correct me if you have understood anything wrong! The next blog will continue to describe how struts solves the above problems and its implementation principles!

 



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.