三层MVC开发模式架构
使用自己设计的商城实体类创建对应的数据库表和类完成增删改查管理模块。 (1)不使用MVC设计模式完成增删改查管理功能。 (2)使用MVC设计模式完成增删改查管理功能。 (3)对比(1)和(2)理解使用MVC模式的优势。
一.先建立目标类对象:Curriculum。
属性对象为:ID,curriculumName,price,introduction。
二.建立DAO层,作用于对于Mysql数据库的增删改查的操作。 对于servlet与数据库操作CURD之间写了一个FService,便于代码的维护和排除管理。具体代码如下: CurriculumDao代码如下:
public List<Curriculum> GetAllCurriculum() throws SQLException {
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from curriculum";
return r.query(sql,new BeanListHandler<Curriculum>(Curriculum.class));
}
public Curriculum selectTypeNameByID(int feid) throws SQLException {
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from curriculum where id=?";
return r.query(sql,new BeanHandler<Curriculum>(Curriculum.class),feid);
}
public Curriculum select(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from curriculum where id = ?";
return r.query(sql, new BeanHandler<Curriculum>(Curriculum.class),id);
}
public void insert(Curriculum f) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into curriculum(curriculumName,price,introduction) values(?,?,?)";
r.update(sql,f.getCurriculumName(),f.getPrice(),f.getIntroduction());
}
public void update(Curriculum f) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update curriculum set curriculumName=?,price=?,introduction=? where id = ?";
r.update(sql,f.getCurriculumName(),f.getPrice(),f.getIntroduction(),f.getId());
}
public void delete(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from curriculum where id = ?";
r.update(sql,id);
}
public static void main(String[] args) throws SQLException {
CurriculumDao td=new CurriculumDao();
List<Curriculum> list=td.GetAllCurriculum();
//System.out.println("size="+list.size());
}
FService代码如下:
CurriculumDao tDao=new CurriculumDao();
public List<Curriculum> GetAllCurriculum()
{
List<Curriculum> list=null;
try {
list=tDao.GetAllCurriculum();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Curriculum selectTypeNameByID(int feid)
{
Curriculum type=null;
try {
type=tDao.selectTypeNameByID(feid);
} catch (SQLException e) {
e.printStackTrace();
}
return type;
}
public Curriculum select(int id) {
Curriculum f=null;
try {
f = tDao.select(id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
public void insert(Curriculum f) {
try {
tDao.insert(f);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(Curriculum f) {
try {
tDao.update(f);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean delete(int id) {
try {
tDao.delete(id);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
三,过滤层Filter
过滤器是指“过滤器”,它是客户端和服务器资源文件之间的过滤器。 在访问资源文件之前,通过一系列过滤器对请求进行修改和判断,对不符合规则的请求进行拦截或中途修改;还可以过滤、拦截或修改响应。 作用:用来过滤网站的数据。 处理中文乱码 登录验证等等
具体代码如下:
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");//解决请求过程的乱码
chain.doFilter(req, resp);
}
需要注意,我们配置注册url映射写一下任何请求访问都需要经过过滤层,这样就实现了对于中文字符的过滤与转码。
1.@WebFilter(filterName = "EncodeFilter",urlPatterns = "/*")
四.增加商品
写一个form表单,写三个输入框,获取到课程名称,课程价格,课程介绍。 传值到映射路径为Curriculum_add的servlet中。
4.1 增加前端adds.jsp
<form class="form-inline" method="post" action="/MVC_war_exploded/Curriculum_add">
课程名称:<input type="text" class="form-control" id="input_curriculumName" name="curriculumName" placeholder="名称" required="required" ><br/>
课程价格:<input type="text" class="form-control" id="input_price" name="price" placeholder="价格" required="required" ><br/>
课程介绍:<input type="text" class="form-control" id="input_introduction" name="introduction" placeholder="课程" required="required" ><br/>
<input type="submit" class="btn btn-warning" value="添加课程教程"/>
4.2 增加的servlet服务
获取到前端发来的带参请求,传递给fService里然后调用Dao层,实现了对于数据项Mysql里添加数据的操作。
- private FService fService = new FService();
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
String curriculumName = request.getParameter("curriculumName");
-
String price = request.getParameter("price");
-
String introduction = request.getParameter("introduction");
-
double price1=Double.parseDouble(price);
-
fService.insert(new Curriculum(curriculumName,price1,introduction));
-
request.getRequestDispatcher("/Curriculum_list").forward(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
doGet(request, response);
- } Adds.jsp->添加我们的目标商品: 发起请求后处理跳转结果页面:
五.删除课程
删除商品的按钮设计在list前端展示所有课程页面里。
1.<a rel="nofollow" class="btn btn-danger" href="/MVC_war_exploded/Curriculum_delete?id=${f.id }">删除</a>
当发生点击事件,向映射路径为Curriculum_delete的servlet发起了一个带参请求。 Curriculum_delete的servlet代码如下: 由FService调用DAO层执行删除sql语句的操作:
private FService fService = new FService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
fService.delete(id);
request.getRequestDispatcher("/Curriculum_list").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
删除执行结果:
五.修改操作
修改和删除大致相同,在list页面里加一个修改的按钮如下:
<a rel="nofollow" class="btn btn-primary" href="/MVC_war_exploded/Curriculum_editshow?id=${f.id }">修改</a>
当发生点击事件,向映射路径为Curriculum_editshow的servlet发起了一个带参请求。
int id = Integer.parseInt(request.getParameter("id"));
fService.delete(id);
request.getRequestDispatcher("/Curriculum_list").forward(request, response);
删除之后:
六.修改课程
修改课程作为一个独立的页面,使用一个form搜集需要改的目标值,然后指向路径为Curriculum_edit的servlet执行修改操作:
<form class="form-horizontal" action="/MVC_war_exploded/Curriculum_edit" method="post">
<input type="hidden" name="id" value="${f.id }">
<div class="form-group">
<div class="col-sm-6">
课程名称:<input type="text" class="form-control" id="input_curriculumName" name="curriculumName" value="${f.curriculumName }" required="required"><br/>
课程价格:<input type="text" class="form-control" id="input_price" name="price" value="${f.price }" required="required"><br/>
课程介绍:<input type="text" class="form-control" id="input_introduction" name="introduction" value="${f.introduction }" required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="submit" class="btn btn-success">提交修改</button>
</div>
</div>
</form>
Curriculum_edit服务:
Curriculum t = new Curriculum();
try {
BeanUtils.copyProperties(t, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
fService.update(t);//更新修改的内容
request.getRequestDispatcher("/Curriculum_list").forward(request, response);
七.查询全部 前端JSP如下:
<c:forEach items="${list }" var="f">
<tr>
<td><p>${f.id }</p></td>
<td><p>${f.curriculumName }</p></td>
<td><p>${f.price }</p></td>
<td><p>${f.introduction }</p></td>
<td>
<a rel="nofollow" class="btn btn-primary" href="/MVC_war_exploded/Curriculum_editshow?id=${f.id }">修改</a>
<a rel="nofollow" class="btn btn-danger" href="/MVC_war_exploded/Curriculum_delete?id=${f.id }">删除</a>
</td>
</tr>
</c:forEach>
Servlet代码如下:
List<Curriculum> list= fService.GetAllCurriculum();
request.setAttribute("list", list);
this.getServletContext().removeAttribute("Curriculum");
this.getServletContext().setAttribute("Curriculum",list);
request.getRequestDispatcher("/Curriculum_list.jsp").forward(request, response);
实验总结
对比(1)和(2)理解使用MVC模式的优势:分工明确,便于维护,重用性高,多个视图共享一个模型,有利于分工合作,可以加快开发进度。 MVC架构图如下:
实验过程中没有遇到较大的问题,在使用Maven的时候遇到过包与sql版本不兼容的问题,通过更换本地包解决了此问题。 本次实验让我对MVC三层架构有了非常深刻的理解,对后面非原生的web开发过程中的推动作用很大。