教育改变生活

标题: MVC项目新闻系统:修改 [打印本页]

作者: 却尘    时间: 2023-6-29 12:55
标题: MVC项目新闻系统:修改
新闻系统-修改
页面效果:
1、前端:updatedetail.jsp
<%@page import="Service.ArticleBiz"%>
<%@page import="POJO.Article"%>
<%@page import="com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import"%>
    <%@page language = "java" import = "java.util.*" pageEncoding = "utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()+":"+
    request.getServerPort() + path +"/";
   %>
   <% Article article = (Article)request.getAttribute("article"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
修改新闻<br>
<form action="ArticleUpdateDo" method = "post">
     <input type = "hidden" name = "id" value = "<%= article.getId()%>"/>标题
     <input type = "text" name = "title" value = "<%= article.getTitle()%>"/><br>作者
     <input type = "text" name = "author" value = "<%= article.getAuthor()%>"/><br>内容
     <textarea name = "content" rows = "5"cols="50"><%= article.getContent() %></textarea><br>
     <input type = "submit" value = "保存修改">
</form>
<a href = "ArticleShowList">返回新闻列表</a>
</body>
</html>
2、控制层(Controller)代码
跳转:ArticleUpdate.java
package Controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import POJO.Article;
import Service.ArticleBiz;
@WebServlet("/ArticleUpdate")
public class ArticleUpdate extends HttpServlet {
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                int id = Integer.parseInt(request.getParameter("id"));
                ArticleBiz articleBiz =new ArticleBiz();
                Article article = articleBiz.get(id);
                request.setAttribute("article", article);
                request.getRequestDispatcher("updatedetail.jsp").forward(request, response);
                }
        }
保存:ArticleUpdateDo.java
package Controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import POJO.Article;
import Service.ArticleBiz;
@WebServlet("/ArticleUpdateDo")
public class ArticleUpdateDo extends HttpServlet {
        @Override
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                request.setCharacterEncoding("utf-8");
                Article article = new Article ();
                article.setId(Integer.parseInt(request.getParameter("id")));
                article.setTitle (request.getParameter("title"));
                article.setAuthor(request.getParameter("author"));
                article.setContent (request.getParameter("content"));
                ArticleBiz articleBiz =new ArticleBiz();
                articleBiz.update(article);
                request.getRequestDispatcher("index.jsp").forward(request, response);
                }
        }






欢迎光临 教育改变生活 (http://bbs.goldoar.com/) Powered by Discuz! X3.2