Simple introduction to the Struts Framework (I): a simple example of MVC code

Source: Internet
Author: User

First look at the MVC pattern flowchart (in fact, the MVC design pattern is model2 in Java .) :



As shown in the figure, the C layer mainly controls page jumps at the servlet layer. The m layer is the specific service processing logic, and the JSP layer is the so-called v layer. MVC is different from what we call three layers. We usually call three layers as the UI Layer, BLL layer, and Dal layer. Specific differences




As shown in the figure, JSP and Servlet constitute the UI Layer, and the model layer is divided into the BLL layer and the Dal layer (that is, the business logic and Data Persistence Layer ).


After recognizing the MVC design pattern in theory, the following is a sample code of the MVC design pattern:


JSP index page index. jsp:

<% @ Page Language = "Java" contenttype = "text/html; charset = gb18030" <br/> pageencoding = "gb18030" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <br/> <HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb18030 "> <br/> <title> insert title here </title> <br/> </pead> <br/> <body> <br/> <form action = "Servlet/adduser. action "method =" Post "> <br/> Name: <input type = "text" name = "username"> <br/> <input type = "Submit" value = "Submit"> <br/> </form> </ p> <p> </body> <br/> </ptml> <br/>


Business Logic code usermanager:


Package COM. cjq. servlet; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> public class usermanager {</P> <p> Public void adduser (string username) {<br/> system. out. println ("usermanager. addusre () ---> Username: "+ username); <br/>}</P> <p> Public void deluser (string username) {<br/> system. out. println ("usermanager. deluser () ---> Username: "+ username); <br/>}</P> <p> Public void modifyuser (string username) {<br/> system. out. println ("usermanager. modifyuser () ---> username "+ username); <br/>}</P> <p> public list queryuser (string username) {<br/> system. out. println ("usermanager. queryuser () ---> username "+ username); <br/> List userlist = new arraylist (); <br/> userlist. add ("A"); <br/> userlist. add ("B"); <br/> userlist. add ("C"); <br/> return userlist; <br/>}< br/>


Servlet control code:

Package COM. cjq. servlet; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. list; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class testservlet extends httpservlet {</P> <p> protected void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {</P> <p> string requesturi = request. getrequesturi (); <br/> system. out. println ("request =" + requesturi); <br/> string Path = requesturi. substring (requesturi. indexof ("/", 1), requesturi. indexof (". "); <br/> system. out. println ("Path =" + path); </P> <p> string username = request. getparameter ("username"); <br/> usermanager = new usermanager (); <br/> // usermanager. adduser (username); <br/> string forward = ""; <br/> If ("/servlet/deluser ". equals (PATH) {<br/> usermanager. deluser (username); <br/> forward = "/del_success.jsp"; <br/>} else if ("/servlet/adduser ". equals (PATH) {<br/> usermanager. adduser (username); <br/> forward = "/add_success.jsp"; <br/>} else if ("/servlet/modifyuser ". equals (PATH) {<br/> usermanager. modifyuser (username); <br/> forward = "/modify_success.jsp"; <br/>} else if ("/servlet/queryuser ". equals (PATH) {<br/> List userlist = usermanager. queryuser (username); <br/> request. setattribute ("userlist", userlist); <br/> forward = "/query_success.jsp "; <br/>}else {<br/> throw new runtimeexception ("request failed"); <br/>}< br/> request. getrequestdispatcher (forward ). forward (request, response); </P> <p >}< br/>


This servlet code mainly implements the function to determine which operations are performed by the page request server, and then calls the business logic to implement the corresponding business operations.

 

Configure servlet:


<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: Web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "webapp_id" version = "3.0"> <br/> <display-Name> test_servlet </display-Name> <br/> <welcome- file-List> <br/> <welcome-File> index.html </welcome-File> <br/> <welcome-File> index.htm </welcome-File> <br/> <welcome-File> index. JSP </welcome-File> <br/> <welcome-File> default.html </welcome-File> <br/> <welcome-File> default.htm </welcome-File> <br/> <welcome-File> default. JSP </welcome-File> <br/> </welcome-file-List> </P> <p> <servlet> <br/> <servlet-Name> testservlet </servlet-Name> <br/> <servlet-class> COM. cjq. servlet. testservlet </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> testservlet </servlet-Name> <br/> <URL-pattern> *. action </url-pattern> <br/> </servlet-mapping> </P> <p> </Web-app>

 

Output result:

The above example has a preliminary understanding of the MVC design pattern. In fact, this example is the basis for learning the Struts framework, only by clarifying this instance can we figure out the implementation principle of the Struts Framework and the use of the Struts framework.

 

So how can we introduce the Struts framework through this example? This problem starts with if-eles.

 

First, we can see that many if-else statements appear in testservlet, which is very unstable. Such programs are very inflexible and will be changed in the future, therefore, the maintenance is very poor, and a large number of strings appear in the IF-Else, so that errors may occur during coding, which may cause trouble for debugging. Therefore, removing if-else is the first step of restructuring and the first step of Struts framework learning.

 

How can I remove if-Else? Please refer to the next article "Learn Struts framework in a simple way (2): Restructuring the Redirect path and business logic in MVC code".


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.