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

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)

Struts ' second blog "SSH Advanced path" struts Fundamentals + Implementation Simple Login (b), we introduced the basic theory of MVC and Struts, the "SSH Advanced path" step-by-step refactoring MVC implementation of the struts framework-starting with a simple MVC (c), We implemented an MVC model, this blog we solve the first problem left by the previous blog: Packaging business logic and jump path.

Let's take a look at the Testservlet code in the previous blog:

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);}}

From the testservlet we found that it is a simple servlet, which is the front controller of struts, is responsible for intercepting all the URLs, through the IF statement according to the different URL to perform related functions, and add, delete, The modifications and queries have their own business logic and jump paths, and we can encapsulate the respective business logic and jump paths into different classes and abstract out a common interface as follows:




Below, let's look at the code for the interface and implementation class:


Interface action

Package Com.liang.servlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public interface Action {public String execute (httpservletrequest req, HttpServletResponse resp) throws Exception;}

Add User Adduseraction

Package Com.liang.servlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Adduseraction implements Action {@Overridepublic String execute ( HttpServletRequest req, HttpServletResponse resp) throws Exception {//Get parameter string username = Req.getparameter ("username" ); Usermanager Usermanager = new Usermanager ();//Invoke Business logic usermanager.add (username);//Return to jump page return "/add_success.jsp";}}

Delete User Deluseraction

Package Com.liang.servlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Deluseraction implements Action {@Overridepublic String execute ( HttpServletRequest req, HttpServletResponse resp) throws Exception {<span style= "White-space:pre" ></span> Gets the parameter string username = Req.getparameter ("username"); Usermanager Usermanager = new Usermanager ();//Invoke Business logic Usermanager.del (username);//Return to jump page return "/del_success.jsp";}}

Modify User Modifyuseraction

Package Com.liang.servlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Modifyuseraction implements Action {@Overridepublic String Execute (httpservletrequest req, HttpServletResponse resp) throws Exception {//Get parameter string username = Req.getparameter (" Username ");//Invoke business logic Usermanager Usermanager = new Usermanager (); usermanager.modify (username);//Return to jump page return"/modify _success.jsp ";}}

Query User Queryuseraction

Package Com.liang.servlet;import Java.util.list;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Queryuseraction implements Action {@Overridepublic String Execute (HttpServletRequest req, HttpServletResponse resp) throws Exception {//Get parameter string username = Req.getparameter ("username ");//Invoke business logic Usermanager Usermanager = new Usermanager (); List userlist = usermanager.query (username); Req.setattribute ("UserList", userlist); Usermanager.query (username);// Return to the jump page return "/query_success.jsp";}}

let's look at the Testservlet after our package.

Package Com.liang.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.liang.action.action;import com.liang.action.AddUserAction; Import Com.liang.action.deluseraction;import Com.liang.action.modifyuseraction;import com.liang.action.queryuseraction;/** * Use servlet to do related control, turn to multiple (V) view * @author Liang * */public class Testservlet extends Htt Pservlet {@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); Action action = null;//equals Add, call adduseractionif ("/servlet/adduser". Equals (Path)) {action = new adduseraction ();//equals Delete, Call Deluseraction}else if ("/servlet/delUser ". Equals (path) {action = new deluseraction ();//equals modify, call Modifyuseraction}else if ("/servlet/modifyuser ". Equals ( Path) {action = new modifyuseraction ();//equals query, call Queryuseraction}else if ("/servlet/queryuser". Equals (Path)) {action = New Queryuseraction ();} else {throw new RuntimeException ("request Failed");} String forward = null;//Returns a different steering page try {forward = Action.execute (request, response);} catch (Exception e) {E.printstacktrac E ();} Complete the steering Request.getrequestdispatcher (forward) according to the path. Forward (request, response); @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doget (request,response);}}

By contrast, we can find a lot simpler, but the realization of the truth is basically the same, still is to intercept the path, make the corresponding judgment, into the corresponding branch, new a just write action,new who tune who, but at this time just finished the function, the system is not flexible, IF ... else has not been removed, the scalability is not good, cannot apply the demand.


Next Blog "SSH Advanced Path" step-by-step refactoring MVC implementation Struts framework-completely remove the judgment logic (v), we solve the second problem: IF ... else's problem.


SOURCE download



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

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.