javaweb中MVC模式

本文通过具体实例介绍MVC模式在Java Web开发中的应用,包括模型(Model)、视图(View)和控制器(Controller)各部分的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MVC模式在编程中能够提供能很大的好处,所以很多的项目中一般都采用这种方法。

下面我们将以javabean来进行MVC的学习,之前我每一篇文章都截图,以便于能够更加清楚执行效果,帮助大家理解,但是我刚才发现,这些图片在编辑文章的时候可以直接粘贴,我排版好之后发布出去,点开之后一张图片都没有了,真是伤心。所以接下来的文章中,我都尽量不配图了。大家如果有什么不懂的地方,评论区我会回答。

  • 首先我们需要建立一个javabean的类,该类中一定要包含无构造参数的构造函数(相当于M)。
package com;

public class Customer {
	private String username;
	private String password;
	
	public Customer(){};//不带参数的构造函数
	//带参数的构造函数
	public Customer(String username,String password){
		this.username = username;
		this.password = password;
	}
	//getter方法
	public String getusername(){return this.username;};
	public String getpassword(){return this.password;};
	//setter方法
	public void setusername(String username){this.username = username;};
	public void setpassworde(String password){this.password = password;};
}

  • 然后编写登陆页面:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>login</title>
    </head>
    
    
    <!-- 页面的样式文件 -->
    <style type="text/css">
    body{
    	background-color : #eee;
    }
    #first{
    	width:500px;
    	margin:30px auto;
    }
    </style>
    
    <body>
    <div id="first">
    	${sessionScope.message}<br><br>
    	<form action="login" method="post" >
    		<table>
    		<tr>
    			<td>用户名:</td>
    			<td><input type="text" name="username"></td>	
    		</tr>
    		<tr>
    			<td>密 码:</td>
    			<td><input type="password" name="password"></td>	
    		</tr>
    		<tr>
    			<td><input type="checkbox" name="check" value="check"/>自动登录<br></td>	
    		</tr>
    		<tr>
    			<td><input type="submit" value="确认"></td>
    			<td><input type="reset" value="取消"></td>	
    		</tr>
    		</table>
    	</form>
    </div>
    </body>
    </html>

  • 在login.java 中使用javabean的实例customer来进行参数传递(相当于C)
package com;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;;

@WebServlet(name="/Login",urlPatterns={"/login"})
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Login() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//构建类对象
		Customer customer = new Customer(username,password);
		
		HttpSession session = request.getSession();
		synchronized(session){
			session.setAttribute("customer", customer);
		}
		RequestDispatcher rd = request.getRequestDispatcher("/show.jsp");
//		RequestDispatcher rd = request.getRequestDispatcher("/ELshow.jsp");
		rd.forward(request, response);
	}
}

  • 最后编写一个显示界面的jsp文件实现显示功能(相当于V):
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="customer" class="com.Customer" scope="session">
<jsp:setProperty name="customer" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>show</title>
</head>
<body>
<h2>用户信息如下:</h2>
<table boder="1">
	<tr>
		<td>用户名:</td>
		<td><jsp:getProperty name="customer" property="username"/></td>
	</tr>
	<tr>
		<td>密码:</td>
		<td><jsp:getProperty name="customer" property="password"/></td>
	</tr>
</table>
</body>
</html>

然后在页面上进行URL访问,就会得到用户的信息。这里不进行图片显示(希望优快云能够解决快速图片的问题)。

这样的MVC的模式进行代码编写的时候有极大的好处,每一个功能块都分开,能够让不同的人进行不同的代码编写更高效的完成任务。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值