在jsp中应用MVC模型

在这里通过一个例子来把MVC的思路理清一下;M:即模型层;V:即视图层;C:即控制层;

在jsp中;jsp页面是视图层。而javabean是模型层,Servlet是控制层:

比如我在数据里有一个文章的表,表的字段有:Artical_Id,Artical_Name ,Artical_Content,Artical_Date,Artical_Glancenumber,ArticalTypel_Id ,User_Id,Artical_Character

创建一个javabean类来封装表的字段;

package com.yxq.valuebean;

import java.util.Date;

public class Model_Artical {
private Integer Artical_Id;
private String Artical_Name ;
private String Artical_Content;
private Date Artical_Date ;
private Integer Artical_Glancenumber ;
private Integer ArticalTypel_Id ;
private Integer User_Id;
private String Artical_Character;
public String getArtical_Character() {
return Artical_Character;
}
public String getArtical_Character(int number) {
if(Artical_Character.length()>30){
return Artical_Character.substring(0, number);
}
return Artical_Character;
}
public void setArtical_Character(String artical_Character) {
Artical_Character = artical_Character;
}
public Integer getArtical_Id() {
return Artical_Id;
}
public void setArtical_Id(Integer artical_Id) {
Artical_Id = artical_Id;
}
public String getArtical_Name() {
return Artical_Name;
}
public void setArtical_Name(String artical_Name) {
Artical_Name = artical_Name;
}
public String getArtical_Content() {
return Artical_Content;
}

public void setArtical_Content(String artical_Content) {
Artical_Content = artical_Content;
}
public Date getArtical_Date() {
return Artical_Date;
}
public void setArtical_Date(Date artical_Date) {
Artical_Date = artical_Date;
}
public Integer getArtical_Glancenumber() {
return Artical_Glancenumber;
}
public void setArtical_Glancenumber(Integer artical_Glancenumber) {
Artical_Glancenumber = artical_Glancenumber;
}
public Integer getArticalTypel_Id() {
return ArticalTypel_Id;
}
public void setArticalTypel_Id(Integer articalTypel_Id) {
ArticalTypel_Id = articalTypel_Id;
}
public Integer getUser_Id() {
return User_Id;
}
public void setUser_Id(Integer user_Id) {
User_Id = user_Id;
}
}

jsp页面中请求Servlet,在某个Servlet里这样写;

package com.yxq.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yxq.dao.ArticalDao;
import com.yxq.dao.ArticaltypeDao;
import com.yxq.dao.UserDao;
import com.yxq.toolsbean.Mytools;
import com.yxq.valuebean.Model_Artical;
import com.yxq.valuebean.Model_ArticalType;

public class ArticleServlet extends HttpServlet {
public ArticleServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
if(action ==null){
action ="";
}
if(action.equals("read")){
System.out.println("方法");
this.readArticle(request, response);
}
if(action.equals("add")){
this.addArticle(request, response);
}
if(action.equals("delete")){
this.deleteArticle(request, response);
}
if(action.equals("modify")){
this.modifyArticle(request, response);
}

}
public void init() throws ServletException {

}
public void readArticle(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
HttpSession session = request.getSession();
String strId = request.getParameter("id");
int id =Mytools.strToint(strId);
System.out.println(id);
ArticalDao articaldao = new ArticalDao();
Model_Artical artical = null;
UserDao userid = new UserDao();
try {
artical= articaldao.queryArticleSingle(id);
Integer userid1 = artical.getUser_Id();
String authorname = userid.getusername(userid1);
request.setAttribute("authorname",authorname);
session.setAttribute("artical",artical);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("/front/ArticleSingle.jsp");
rd.forward(request, response);

}
public void addArticle(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
request.setCharacterEncoding("utf-8");
ArticalDao articledao = new ArticalDao();
Model_Artical article =new Model_Artical();
String articlecontent , articletitle;
try{
articlecontent= request.getParameter("articlecontent");
articletitle = request.getParameter("articletitle");
article.setArtical_Content(articlecontent);
article.setArtical_Glancenumber(0);
article.setArtical_Name(articletitle);
article.setArticalTypel_Id(1);
article.setUser_Id(1);
article.setArtical_Date(new Date());
articledao.operattionArtical("add", article);

}catch(Exception e){
e.printStackTrace();
System.out.println("获取表单信息出错");

}
response.sendRedirect(request.getContextPath()+"/admin/MyJsp.jsp");
}
public void deleteArticle(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String strId = request.getParameter("id");
int id =Mytools.strToint(strId);
System.out.println("删除文章的id"+id);
System.out.print("删除文章");
ArticalDao articaldao = new ArticalDao();
try {
articaldao.deleteartical(id);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("删除文章出错");
e.printStackTrace();
}
response.sendRedirect(request.getContextPath()+"/admin/admin.jsp");
}
/*修改文章*/
public void modifyArticle(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String strId = request.getParameter("id");
int id =Mytools.strToint(strId);
System.out.println("修改文章的id"+id);
System.out.print("修改文章");
ArticalDao articaldao = new ArticalDao();
Model_Artical artical = null;
ArticaltypeDao articaltypedao = new ArticaltypeDao();
try {
artical= articaldao.queryArticleSingle(id);
request.setAttribute("artical",artical);
request.setAttribute("articalid",id);
List<Model_ArticalType> articaltype = articaltypedao.query();
request.setAttribute("articletypelist",articaltype);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("/admin/modifySingle.jsp");
rd.forward(request, response);
}
}

时间: 2024-10-06 01:19:56

在jsp中应用MVC模型的相关文章

JAVA Web中的MVC模型

在学习完servlet之后,老师讲解了一个MVC模型,将我们练习的项目进行升级,之前我们的代码全是在jsp里面写的,也就是说网页里的有大量的java代码.在写页面的逻辑的时候很方便,在网页里直接写,写完下面就能用,但是里面的逻辑处理太多,这就是一个大问题,任何人都可更改源码使结果发生改变,这样设计出来的网页没有任何安全性而言. 升级后的项目采用的是最普通的MVC模型,即一个请求一个servlet,但是这样重复代码太多,开发效率大大降低,在这个基础之上,老师讲了一个MVC模型,将servlet当作

spring mvc EL ModelAndView的 Model 值 在jsp中不显示

问题:spring mvc开发过程中, 经常会给model addAttribute, 然后通过EL在jsp中显示,比如 ${msg}, 但是有时候会出现jsp最后显示的还是${msg},而不是msg赋值过的. Spring Controller import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.serv

带你初识Angular中MVC模型

简介 MVC是一种使用 MVC(Model View Controller 模型-视图-控制器)设计模式,该模型的理念也被许多框架所吸纳,比如,后端框架(Struts.Spring MVC等).前端框架(Angular.Backbone等).在学习angular的过程中,我在网上查找关于angular MVC介绍的文章很少,有些文章也没有很直白地为初学者指明angular MVC到底是啥样貌,因此,今天我们就来谈谈MVC模型在angular的形态. 为了介绍angular MVC模型,我建立一个

Notifications:关于MVC模型中的&#39;radio station&#39;

转载请注明来源,谢谢——by Guava Notification 既iOS中的“通知”(广播站)机制可以用来监听系统.模型的变化. 接收端: 通过 [NSNotificationCenter defaultCenter] 获得一个共享实例,就用这个对象来接收广播. 收听广播的方法是,向对象发送消息: - (void)addObserver:(id)observer // you (the object to get notified) 收听广播的对象,对一般的controller来说是self

Spring MVC:在jsp中引入css

为了将css引入jsp中,今天可真是踩了好多坑,最后在stackoverflow上找到了解决方法,不多说贴出代码. 在web.xml中添加以下代码: <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> spring-servlet.xml配置如下: <?xml vers

Django框架进阶5 models常用字段及参数, choices参数, 自动显示sql命令配置, orm查询优化相关, orm中的事务操作, MTV与MVC模型, 图书管理系统(图书的增删改查)

models中的常用字段 AutoField(primary_key=True) 主键   (int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列.) CharField(max_length=32)     varchar(32) IntegerField()       int BigIntergerField()           bigint DecimalField()    decimal EmailField(

MVC模型

当客户端提交请求至服务端时,Struts2的FileterDispatcher(核心控制器)会负责拦截,并且内置拦截器会负责对请求中的参数做一系列的预处理工作(包含:解析参数.类型转换.DTO封装等).然后提交给具体的Action(业务控制器)调用业务实现直至返回处理结果,最后Action根据处理结果forward到具体的View层组件. 一.MVC模型介绍 MVC(Model.View.Controller)是在程序开发过程中衍生出的一套程序设计思想,它由三个单词的缩写组成,分别为:模型层(M

(转)Spring3MVC 在JSP中使用@ModelAttribute

原文连接:http://liukai.iteye.com/blog/973717,仅供自己参考. 在教程中,我们将创建一个简单的Spring3MVC simple CRUD应用程序. 什么是@ModelAttribute  Spring3关于@ModelAttribute的文档 引用 @ModelAttribute has two usage scenarios in controllers. When you place it on a method parameter, @ModelAttr

[原创]java WEB学习笔记18:java EE 中的MVC 设计模式

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 ---------------------------------