在这里通过一个例子来把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);
}
}