SSM项目实践

作业任务:

使用ssm开发新闻网页。

一.新建mavenx项目:

SSM项目实践

 

 2.输入项目名创建项目:

SSM项目实践

 

 3.创建项目:

SSM项目实践

 

 

4.开始配置:

Maven导入需要的包:

SSM项目实践

 

 SSM项目实践

 

 

5.配置后的目录如下:

SSM项目实践

 

 

二.配置文件:

Spring配置文件:applicationContext.xml:

SSM项目实践

 

 SSM项目实践

 

 

 

 

Springmvc.xml:

SSM项目实践

 

 

三.项目代码:

在controller下新建一个Newcontroller 类,进行新闻的删除,添加,更新,查询。

 

package com.controller;

import com.entity.News;
import com.entity.User;
import com.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class NewsController {

    @Autowired
    private NewsService newsService;

    //显示所有新闻
    @RequestMapping("/ShowNews")
    public void ShowNews(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            List<News> newsList = newsService.QueryNews();
            for (News s:newsList
            ) {
                System.out.println(s.getNewsId());
            }

            request.setAttribute("News", newsList);
            request.getRequestDispatcher("ShowNews.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/AddNews")
    public void addNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Context-Type","text/html;charset=utf-8");
        News news =new News();
        news.setNewsId(Integer.valueOf(request.getParameter("newsid")));
        news.setNewsAuthor(request.getParameter("author"));
        news.setNewsContent(request.getParameter("Content"));
        news.setNewsTitle(request.getParameter("title"));
        NewsService newsService=new NewsService();
        try {
            newsService.AddNews(news);
            request.getRequestDispatcher("ShowNews").forward(request,response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/UpdateNews")
    public void editNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Context-Type", "text/html;charset=utf-8");
        News news = new News();
        news.setNewsId(Integer.valueOf(request.getParameter("newsid")));
        news.setNewsAuthor(request.getParameter("author"));
        news.setNewsContent(request.getParameter("Content"));
        news.setNewsTitle(request.getParameter("title"));
        NewsService newsService = new NewsService();
        try {
            newsService.UpdateNews(news);
            request.getRequestDispatcher("ShowNews").forward(request, response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/deleteNew")
    public void deleteNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        int newsID=Integer.valueOf(request.getParameter("newsid"));
        NewsService newsService=new NewsService();
        try {
            newsService.DeleteNews(newsID);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("com.controller.ShowNewsServlet").forward(request,response);
    }

    @RequestMapping("/viewNew")
    public void viewNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int newsID=Integer.valueOf(request.getParameter("newsid"));
        NewsService newsService=new NewsService();
        try {
            News news=newsService.QueryIndividualNews(newsID);
            request.setAttribute("news",news);
            request.getRequestDispatcher("NewsDetail.jsp").forward(request,response);
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}

 

 

2.新建一个登录的类:

 

package com.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@Controller
public class UserController {

    @RequestMapping("/Login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Context-Type", "text/html;charset=utf-8");

        PrintWriter output = response.getWriter();
        String uname = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        ResultSet rs;
        Statement statement = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8", "root", "Guozhaojie610");
            String sql = "select * from new_schema.login where username='" + uname + "' and password='" + pwd + "'";
            statement = conn.createStatement();
            rs = statement.executeQuery(sql);
            if (rs.next()) {
                request.setAttribute("username", uname);
                response.sendRedirect("index.html");
            } else {
                output.println("账号或密码错误");
                response.sendRedirect("index.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

添加一个用户登录的类:

package com.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    String username;
    String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) { this.username = username; }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 

添加一个News 类:

package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class News {
    private int newsId;
    private String newsContent;
    private  String newsAuthor;
    private  String newsTitle;

    public int getNewsId() {
        return newsId;
    }

    public void setNewsId(int newsId) {
        this.newsId = newsId;
    }

    public String getNewsContent() {
        return newsContent;
    }

    public void setNewsContent(String newsContent) {
        this.newsContent = newsContent;
    }

    public String getNewsAuthor() {
        return newsAuthor;
    }

    public void setNewsAuthor(String newsAuthor) {
        this.newsAuthor = newsAuthor;
    }

    public String getNewsTitle() {
        return newsTitle;
    }

    public void setNewsTitle(String newsTitle) {
        this.newsTitle = newsTitle;
    }

}

 

 

这里就不给出其他的代码了,最终的效果与之前使用servlet的一样。

最终效果截图:

SSM项目实践

 

 

 SSM项目实践

 

 

SSM项目实践

 

 

 

SSM项目实践

 

 

SSM项目实践

 

 

SSM项目实践

 

总结:

这次的作业,我不是很熟悉SSM的知识,所以做得不太好,参考了很多同学写的,效果做得也不是很好。

 

github地址:https://github.com/fengpeng123/SSM-

上一篇:Elasticsearch中文分词+全文搜索demo


下一篇:睛节大一c语言代码大全表解释