SSM项目实践

实验小组成员:

 

(1)郭昭杰  学号:201731062608

 

(2)庞斌  学号:201731062609

 

(3)唐任峻杰  学号:201731062610

 

项目码云地址:https://gitee.com/ashes-g/SSM

 

一、配置SSM框架

创建Maven项目,勾选Create from archetype后,选择maven-archetype-webapp

 

 

 

输入项目名并选择保存位置

 

 

 

继续下一步,使用本地的setting文件和jar包地址

 

 

 

点击finish后项目创建完成,下面开始正式配置

 

 

 

首先要配置文件的目录结构,配置完成后的目录结构如图所示:

 

 

 

注意:我在建包时遇到了无法在com包下建立它的下级包的情况,在下图所示位置取消勾选compact middle packlages选项即可

 

 

修改pom.xml文件,导入需要的包

复制代码


    4.0.0
 
    com.xiao
    SSM
    1.0-SNAPSHOT
    war
 
    
    
        4.2.4.RELEASE
        3.2.8
        1.2.2
        1.6.4
        1.2.17
        2.4.2
        1.0.9
        5.1.32
        1.2
        2.5
        2.0
        1.3.2
        1.3.1
    
 
    
    
        
            
            
                org.springframework
                spring-context
                ${spring.version}
            
            
                org.springframework
                spring-beans
                ${spring.version}
            
            
                org.springframework
                spring-webmvc
                ${spring.version}
            
            
                org.springframework
                spring-jdbc
                ${spring.version}
            
            
                org.springframework
                spring-aspects
                ${spring.version}
            
            
                org.springframework
                spring-jms
                ${spring.version}
            
            
                org.springframework
                spring-context-support
                ${spring.version}
            
 
            
            
                org.mybatis
                mybatis
                ${mybatis.version}
            
            
                org.mybatis
                mybatis-spring
                ${mybatis.spring.version}
            
 
            
            
                org.slf4j
                slf4j-log4j12
                ${slf4j.version}
            
            
                log4j
                log4j
                ${log4j.version}
            
 
            
            
                com.fasterxml.jackson.core
                jackson-databind
                ${jackson.version}
            
 
            
            
                com.alibaba
                druid
                ${druid.version}
            
 
            
            
                mysql
                mysql-connector-java
                ${mysql.version}
            
 
            
            
                org.apache.commons
                commons-io
                ${commons-io.version}
            
 
            
            
                commons-fileupload
                commons-fileupload
                ${commons-fileupload.version}
            
 
            
            
                jstl
                jstl
                ${jstl.version}
            
            
                javax.servlet
                servlet-api
                ${servlet-api.version}
                provided
            
            
                javax.servlet
                jsp-api
                ${jsp-api.version}
                provided
            
        
    
 
    
    
        
        
            org.springframework
            spring-context
        
        
            org.springframework
            spring-beans
        
        
            org.springframework
            spring-webmvc
        
        
            org.springframework
            spring-jdbc
        
        
            org.springframework
            spring-aspects
        
        
            org.springframework
            spring-jms
        
        
            org.springframework
            spring-context-support
        
 
        
        
            org.mybatis
            mybatis
        
        
            org.mybatis
            mybatis-spring
        
 
        
        
            org.slf4j
            slf4j-log4j12
        
 
        
        
            com.fasterxml.jackson.core
            jackson-databind
        
 
        
        
            com.alibaba
            druid
        
 
        
        
            mysql
            mysql-connector-java
        
 
        
        
            org.apache.commons
            commons-io
        
 
        
        
            commons-fileupload
            commons-fileupload
        
 
        
        
            jstl
            jstl
        
        
            javax.servlet
            servlet-api
            provided
        
        
            javax.servlet
            jsp-api
            provided
        
    
 
    
        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                
            
        
 
        
        
            
                src/main/java
                
                    **/*.xml
                
                false
            
 
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
 
    
 
复制代码

 

修改完成后,在项目名处右击,选择Maven-Reimport即可完成引入

 

 

 

下一步,在resource目录进行配置文件的添加,共4个文件

(1)连接数据库配置信息文件:db.properties

druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/all_db?characterEncoding=utf-8
druid.username=root
druid.password=Guozhaojie610

(2)Mybatis配置文件:SqlMapConfig.xml

复制代码




    
    
        
    

复制代码

(3)Spring配置文件:applicationContext.xml

 

复制代码



    
    

    
    

    

    
    
    
    
    
    

    
    
        
        
        
        

    

复制代码

 

 

最后需要在web.xml文件中进行配置:

复制代码



  SSM

  
    index.jsp
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
  
    encoding
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    encoding
    /*
  

  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    springmvc
    /
  

复制代码

 

最终的目录结构:

 

 

二、修改项目代码

将原项目的java类、页面文件复制进项目后,需要进行一定的修改使其基于spring框架运行

将原有的各个NewsServlet类集合到一个新建的NewsController类中

复制代码
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 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();
        }
    }
    
}
复制代码

将LoginServlet类改为UserController类

复制代码
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();
        }
    }
}
复制代码

实体类:

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;
    }

}
复制代码

User

复制代码
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;
    }
}
复制代码

 

最后将页面的连接进行重定向,这里不再放出代码

最终项目结构:

 

 

 

 

三、最终效果展示

与MVC版本效果相同

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(SSM项目实践)