JSP的MVC开发模式
1 什么是MVC模式
MVC,代表模型(Model),视图(View),控制器(Controller)。这是一种将业务逻辑,表现逻辑和数据分开的设计模式。
控制器充当视图和模型之间的接口。控制器拦截所有传入的请求。
模型代表应用程序的状态,即数据。它还可以具有业务逻辑。
视图即UI(用户界面)。
2 MVC(Model2)模式的优势
- 多个视图共享一个模型,大大提高代码的可重用性。
- 易于维护的大型应用程序
3 MVC模式的示例
在下面的示例中,我们使用Servlet作为控制器,使用JSP页面作为视图组件,使用Java Bean类作为模型。
3.1 编写index.jsp
<%@ page language="java" contentType="text/html;charset=utf-8" %>
<html>
<html>
<head>
<meta charset="utf-8">
<title>一点教程网-JSP的MVC模式</title>
</head>
<body>
<form action="ControllerServlet" method="post">
用户名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
3.2 编写User实体类
package com.yiidian;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class User {
private String name;
private String password;
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 boolean validate(){
if(password.equals("admin")){
return true;
}
else{
return false;
}
}
}
3.3 编写ControllerServlet
package com.yiidian;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class ControllerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String name=request.getParameter("name");
String password=request.getParameter("password");
User bean=new User();
bean.setName(name);
bean.setPassword(password);
request.setAttribute("bean",bean);
boolean status=bean.validate();
if(status){
RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp");
rd.forward(request, response);
}
else{
RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp");
rd.forward(request, response);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
}
3.4 编写login-success.jsp
<%@ page import="com.yiidian.User" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>一点教程网-JSP的MVC模式</title>
</head>
<body>
<p>你已经成功登录</p>
<%
User bean=(User) request.getAttribute("bean");
out.print("欢迎你, "+bean.getName());
%>
</body>
</html>
3.5 编写login-error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>一点教程网-JSP的MVC模式</title>
</head>
<body>
<p>抱歉! 用户名或密码错误</p>
<form action="ControllerServlet" method="post">
用户名:<input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
3.6 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.yiidian.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>
</web-app>
3.7 运行测试
输入错误的密码,显示如下:
输入正确的密码(admin),显示如下:
热门文章
优秀文章