MVC is what I don't say, we usually do Javaweb project, most will use the different combination of SSH framework, that can not use the SSH framework to implement a native MVC framework?
Let's implement one of our own javaweb MVC frameworks.
The project structure is as follows:
Implementation of Controler:
Package Com.shu.controler;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.shu.action.action;import com.shu.action.actionfactory;/** * Controlerservlet, controller class, all requests that end with a. Action will be blocked by the controller, * For example: Http://localhost:8080/mvc/LoginAction.action * @author Administrator * */public class Controlerservlet extends HttpServlet {/** * */private static final long Serialversionuid = 4660044561652707132L; @Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {doPost (req, resp);} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { String pathName = Req.getservletpath (); System.out.println ("PathName:" + pathName); int index = Pathname.indexof ("."); String actionname = pathname.substring (1,index); System.out.println ("ActionName:" + actiOnname); String Actionclass = Getinitparameter (ActionName); System.out.println ("Actionclass:" + actionclass); Action action = actionfactory.getaction (ActionName); String Viewurl = Action.execute (req, resp), if (Viewurl = = null) {Req.getrequestdispatcher ("error.jsp"). Forward (req, RESP);} else {req.getrequestdispatcher (Viewurl). Forward (req, resp);}}}
Action interface:
Package Com.shu.action;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpServletResponse ;/** * Action interface, all business Action classes need to implement this interface * The Execute method returns a reference string for the view resource, such as "admin/login.jsp" * @author Administrator * */ Public interface Action {public String execute (httpservletrequest request, httpservletresponse response);}
Action Factory:
Package Com.shu.action;import com.shu.actions.loginaction;/** * This factory returns a specific business action instance based on the name of the incoming action * @author Administrator * */public class Actionfactory {public static Action getaction (String actionname) {if (LoginAction.class.ge Tsimplename (). Equals (ActionName)) {return new loginaction ();} return null;}}
User Login Business Action:
Package Com.shu.actions;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import com.shu.action.action;/** * Handle User Login Action class * @author Administrator * */ public class Loginaction implements Action {@Overridepublic String execute (httpservletrequest request, HttpServletResponse response) {String username = request.getparameter ("username"); String Password = request.getparameter ("password"); Request.setattribute ("username", username); Request.setattribute ("Password", password); if (username! = null && Username.trim (). Length ()! = 0 && Password! = null && ; Password.trim (). Length ()! = 0) {return "admin/success.jsp";} return "admin/login.jsp";}}
Xml:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "><servlet><servlet-name> Controlerservlet</servlet-name><servlet-class>com.shu.controler.controlerservlet</servlet-class ><init-param><param-name>LoginAction</param-name><param-value> com.shu.actions.loginaction</param-value></init-param></servlet><servlet-mapping>< servlet-name>controlerservlet</servlet-name><url-pattern>*.action</url-pattern></ Servlet-mapping></web-app>
Views View layer: login.jsp
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 " pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
SUCCESS.JSP:
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 " pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Test:
Implement your native Java EE MVC Framework with JSP and servlet