SSH Step-by-step refactoring MVC implementation struts framework-starting with a simple MVC (iii)

Source: Internet
Author: User

Directory:

"SSH advanced Route" Struts Fundamentals + Implementation Simple Login (ii)

SSH Step-by-step refactoring MVC implementation struts framework-starting with a simple MVC (iii)

"SSH Advanced path" Step by step refactoring MVC implementation Struts framework-package business logic and jump Path (iv)

"SSH Advanced path" Step by step refactoring MVC to implement struts framework--completely remove the logic judgment (v)

"SSH Advanced path" Step by step refactoring MVC implementation struts framework-perfect Steering page, done (vi)

"SSH Advanced path" struts Fundamentals + Implementation Simple Login (b), we introduce the basic theory of MVC and struts, and implement a simple login instance. As we know, struts is the framework of MVC, MVC is actually model2,m represents the model, we can understand the business logic, V represents the view, mainly interface display, using JSP Display, C for control, control the most typical way is to use the servlet, can get parameters, Call the business logic and turn to another interface.

After the introduction of the previous blog, we know that struts to the MVC encapsulation, how struts is encapsulated MVC, has not been resolved, from the beginning of this blog, we step-by-step refactoring MVC implementation of a basic struts framework of the embryonic, Help us to learn more about the basic process of struts to provide assurance. Here we go.

We strictly follow the previous blog Model2 of the timing diagram or, for example, using Jsp+servlet to show you a simple MVC, step-by-step reconstruction, perfect, the framework of an implementation of the people cited.


Hypothesis is a user of the increase, deletion, change, check the function, let's tap the code of each part. Create a new Java Web project, Test_struts1, such as:



First, create a new index page, provide a form, and simply provide a user name, such as implementing the Add user function as the main interface.


index.jsp

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

C, the controller, using Servlets to implement the responsibilities of the Controller, obtain form data, invoke business logic, and turn to the page. The client submits a little to the servlet, and the code is as follows:

Testservlet.java

Package Com.liang.servlet;import Java.io.ioexception;import Java.util.list;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * Use the servlet to do the relevant control and turn to multiple (V) views * @author Liang * */public class Testservlet Extends HttpServlet {@Overrideprotected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//Get access to uristring Reqeuesturi = Request.getrequesturi (); System.out.println (Reqeuesturi);//intercept URI, get path string = reqeuesturi.substring (Reqeuesturi.indexof ("/", 1), Reqeuesturi.indexof (".")); SYSTEM.OUT.PRINTLN (path);//Get form data string username = Request.getparameter ("username"); Usermanager Usermanager = new Usermanager (); String forward = "";//perform related functions according to URL//equals Add, call Add method, add successful go to add page if ("/servlet/adduser". Equals (path)) {// Call added business logic usermanager.add (username); forward = "/add_success.jsp";//delete, invoke Delete method, delete successfully go to delete}else if ("/servlet/deluser". Equals (Path){//Call the deleted business logic Usermanager.del (username); forward = "/servlet/deluser";//Modify, invoke Modify method, modify successfully go to modify page}else if ("/servlet/ ModifyUser. Equals (path) {//Invoke Modified business logic usermanager.modify (username); forward = "/modify_success.jsp";//query, invoke Query method, The query successfully went to the query Success page}else if ("/servlet/queryuser". Equals (path)) {//Call Query business logic list userlist = Usermanager.query (username); Request.setattribute ("UserList", userlist); forward = "/query_success.jsp";} Else{throw New RuntimeException ("request Failed");} Unified Complete Steering Request.getrequestdispatcher (forward). Forward (request, response);} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doget (request,response);}}

The controller needs to invoke the business logic and go to the appropriate page, prompting the user for a success or failure with the following code:


M, business logic, we complete the function of increment, delete, change, check, no longer operate the database, in the form of the control desk printing information, do a simple demonstration.

Usermanager.java

Package Com.liang.servlet;import Java.util.arraylist;import Java.util.list;public class Usermanager {//Add user public void Add (String username) {System.out.println ("Usermanager.add ()-->>>username:" + username);} Delete user public void Del (String username) {System.out.println ("Usermanager.del ()-->>>username:" + username);} Modify user public void Modify (String username) {System.out.println ("usermanager.modify ()-->>>username:" + username);} Query user public List query (String username) {System.out.println ("Usermanager.query ()-->>>username:" + username ); List userlist = new ArrayList (), Userlist.add ("a"), Userlist.add ("B"), Userlist.add ("C"); Userlist.add ("D"); return UserList;}}

to the page, add a successful JSP:

add_success.jsp

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Delete Success page and modify Success page as above, only one sentence represents a different page.

Query Success page, query_success.jsp

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Configuration information Web. xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">  <welcome-file-list>    <welcome-file> index.jsp</welcome-file>  </welcome-file-list>  <!--configuration Testservlet class--  <servlet >  <servlet-name>TestServlet</servlet-name>  <servlet-class> com.liang.servlet.testservlet</servlet-class>  </servlet>  <!--How to access it, consistent with the path of the JSP action-- >  <servlet-mapping>  <servlet-name>TestServlet</servlet-name>  <!-- As long as the. Do request arrives at servlet-  <url-pattern>*.do</url-pattern>  </servlet-mapping>  </web-app>


Finally, build tomacat, deploy access. Console printing information, the successful jump page no longer show, the effect is as follows:




If you want to access other functions of the business logic, you only need to modify the value of the action in the index.jsp. To this end, we have implemented a simple MVC that is used to delete and modify user functions.


Let's look at the Testservlet code above and have a lot of questions:

1. All business logic and jump paths are coupled in the Testservlet class. To add additional requirements, you need to modify the Testservlet, violating the open closure principle .

2, in the servlet there are many if-else statements, if statements are very unstable, always need to change.

If you want to add another query function, you need to modify the servlet to add an IF. Else statement, too cumbersome, violates the principle of open closure, extensibility is not good.

3, all the turn pages are written dead, display and control separation, if you want to change a view, to modify the code.



We will solve all the above problems, and finally realize the prototype of the Struts framework. Next Blog "SSH Advanced Path" step-by-step refactoring MVC implementation Struts framework-package business logic and jump path (iv), first solve our first problem.


SOURCE download



SSH Step-by-step refactoring MVC implementation struts framework-starting with a simple MVC (iii)

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.