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模型如来。性奋....

JSP MVC,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。