自定义 mvc
来源:互联网 发布:java.io.eofexception 编辑:程序博客网 时间:2024/04/29 18:44
实现MVC自定义框架(简单实现登录功能)
一,模型层:
实体类,接口(实体类接口,Action接口),接口实现类(实体类接口实现类,Action接口实现类)
代码:
二、控制器层,ActionServlet类(添加一个获得Action的方法)
web.xml:配置如下
三、页面
一,模型层:
实体类,接口(实体类接口,Action接口),接口实现类(实体类接口实现类,Action接口实现类)
代码:
- package entity;
- /**
- * 用户类
- * @author Administrator
- */
- public class User {
- private String username;//用户名
- private String password;//密码
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
- package dao;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Action接口
- * @author Administrator
- *
- */
- public interface Action {
- public String execute(HttpServletRequest request,HttpServletResponse response);
- }
- package dao;
- import entity.User;
- /**
- * 用户类接口
- * @author Administrator
- */
- public interface UserDao {
- //登录方法
- public User login(String username,String password);
- }
- package dao.impl;
- import dao.UserDao;
- import entity.User;
- /**
- * 用户接口的实现类
- * @author Administrator
- */
- public class UserDaoImpl implements UserDao {
- /**
- * 登录
- */
- public User login(String username, String password) {
- User user=null;
- if(username.equals("admin")&&password.equals("admin")){
- user=new User();
- user.setPassword(password);
- user.setUsername(username);
- }
- return user;
- }
- }
- package dao.impl;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import dao.Action;
- import entity.User;
- /**
- * 登录Action类实现Action接口
- * @author Administrator
- */
- public class LoginAction implements Action {
- public String execute(HttpServletRequest request,
- HttpServletResponse response) {
- //接收参数
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- //创建UserDaoImpl对象
- UserDaoImpl udi=new UserDaoImpl();
- //调用登录方法
- User user=udi.login(username, password);
- //判断 User对象是否为空
- if(user!=null){
- return "success.jsp";
- }else{
- return "error.jsp";
- }
- }
- }
二、控制器层,ActionServlet类(添加一个获得Action的方法)
web.xml:配置如下
- <servlet-name>ActionServlet</servlet-name>
- <servlet-class>servlet.ActionServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ActionServlet</servlet-name>
- <url-pattern>*.action</url-pattern><!-- 过滤所有以.action结尾 -->
- </servlet-mapping>
- package 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 dao.Action;
- import dao.impl.LoginAction;
- public class ActionServlet extends HttpServlet {
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request,response);
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //得到Action对象
- Action action=getAction(request);
- //调用Action实现类方法
- try {
- String result=action.execute(request, response);
- if(result!=null){
- response.sendRedirect(result);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 创建一个获得Action的方法
- * @param request
- */
- public static Action getAction(HttpServletRequest request){
- Action action=null;
- //截取action请求名(例如页面的登录:<form action="login.action" method="post">)
- String uri=request.getRequestURI();
- System.out.println("request.getRequestURI()---"+uri);
- //结果:request.getRequestURI()---/qifei_mvc_kuangjia_zuoye/login.action
- StringBuffer url=request.getRequestURL();
- System.out.println("request.getRequestURL()--"+url);
- //结果:request.getRequestURL()--http://localhost:8080/qifei_mvc_kuangjia_zuoye/login.action
- String contextPath=request.getContextPath();
- //结果:request.getContextPath()--/qifei_mvc_kuangjia_zuoye
- System.out.println("request.getContextPath()--"+contextPath);
- String actionUrl=uri.substring(contextPath.length());
- System.out.println("actionUrl--"+actionUrl);
- String actionName=actionUrl.substring(1,actionUrl.lastIndexOf(".")).trim();
- //判断请求名
- if(actionName.equals("login")){
- action=new LoginAction();
- }
- return action;
- }
- }
三、页面
- <body>
- <form action="login.action" method="post">
- 用户名:<input type="text" name="username"/><br/>
- 密码:<input type="password" name="password"/><br/>
- <input type="submit" value="提交">
- </form>
- </body>
0 0
- 自定义 mvc
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义-MVC
- 自定义mvc
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC
- 自定义MVC框架
- 自定义MVC框架
- mvc自定义控件
- Centos安装mariadb
- python 嵌套函数、闭包、decorator
- Ubuntu下添加开机启动项的2种方法
- 自动增量_自动升级系统_nsAutoUpdate_NSIS_插件发布
- 关于贪心算法
- 自定义 mvc
- 基础拾遗------特性详解【含常用过滤器实例】
- spark sql和DataFrame本质
- Openjudge NOI题库1.13编程基础之综合应用17:文字排版
- bootstrap 模态框modal 传值问题
- maven javadoc plugin build fails with Java 8 when Javadoc tags are incomplete
- 安装YouCompleteMe
- spark sql on hive配置及其使用
- Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()