• JSP MVC


    Java的MVC玩起来比.Net的有意思,因为每一步你都知道它是如何运作的,都由自己去实现。而在.Net的MVC中,你却不知道一个Controller是如何对应到View中的,因为MS给了你IDE上的操作方便,具体内部是如何做的映射,很难找到或不知道。

    Java MVC的原理可有这样理解(网上说是这种):JSP -> Servlet -> JSP

    (个人认为最合适的是这样解释比较好):Servlet -> JSP

    即不需要让用户直接访问JSP。因为这是Resful风格哈。

    上代码例子。

    建一个Bean: User

    package com.my.beans;
    
    import java.lang.*;
    
    public class User {
        private String name;
        private String password;
        private int age;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    
        public User() {}
    }

    建一个JSP:mvctest.jsp

    <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
    
    <html>
        <header>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        </header>
        <body>
            <form action="${pageContext.request.contextPath}/requestattr" method="POST">
                User name: <input type="textbox" id="txtUserName" name="txtUserName" value="${param.txtUserName}" />
                <input type="submit" value="Submit" /><br/>
                <hr />
                User name textbox value: ${param.txtUserName}<br/>
                Bean user name value: ${userBean.name}<br/>
                Bean password value: ${userBean.password}<br/>
                Bean age value: ${userBean.age}<br/>
            </form>
        </body>
    </html>

    建一个Servlet: RequestAttr

    package com.my;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import com.my.beans.User;
    
    public class RequestAttr extends HttpServlet {
        public RequestAttr() {}
        
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            req.getRequestDispatcher("/pages/mvctest.jsp").forward(req, resp);
        }
        
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            PrintWriter out = resp.getWriter();
            
            String strUserName = req.getParameter("txtUserName");
            
            User user = new User();
            user.setName(strUserName);
            user.setPassword("123");
            user.setAge(18);
            
            req.setAttribute("userBean", user);
            req.getRequestDispatcher("/pages/mvctest.jsp").forward(req, resp);
        }
    }

    web.xml修改:

        <servlet>
          <servlet-name>requestattr</servlet-name>
          <servlet-class>com.my.RequestAttr</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>requestattr</servlet-name>
            <url-pattern>/requestattr</url-pattern>
        </servlet-mapping>

    结果:

    访问地址:http://localhost:8080/mytest/requestattr

    一个MVC模型如来。性奋....

  • 相关阅读:
    三元组顺序结构实现稀疏矩阵相加,行序优先(Java语言描述)
    行逻辑链接的顺序表实现稀疏矩阵的相乘(Java语言描述)
    各种矩阵压缩原理(书本上没有讲到的具体公式推理过程)
    KMP算法的实现(Java语言描述)
    链表一元多项式计算器的实现(Java语言描述)
    链表的实现(Java语言描述)
    队列的链式存储方式的实现(Java语言描述)
    Java泛型数组
    三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
    利用栈实现算术表达式求值(Java语言描述)
  • 原文地址:https://www.cnblogs.com/HD/p/3632609.html
Copyright © 2020-2023  润新知