ssm重新开发计科院新闻网站

ssm框架就是:spring+springmvc+mybits。

Spring:
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。

SpringMvc:

SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。

mybits:

mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。

 运行结果:

首页(游客也可浏览)

ssm重新开发计科院新闻网站_第1张图片

登陆后台:

ssm重新开发计科院新闻网站_第2张图片

 

ssm重新开发计科院新闻网站_第3张图片

 

 相关代码:

插入:

 package com.c;

import java.io.IOException;
import java.io.PrintWriter;

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 com.a.DBHelper;

@WebServlet("/Insert")
public class Insert extends HttpServlet {

    /**
         * The doGet method of the servlet. 
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred
*/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置请求和响应的字符集编码 response.setContentType("text/html; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); //获得输出流 String strID = request.getParameter("uid"); String strName = request.getParameter("uname"); DBHelper dbhelper=new DBHelper(); dbhelper.connect(); if(""!=strID&&""!=strName){ dbhelper.add(strID, strName); out.println("插入成功!请返回"); } dbhelper.close(); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred
*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } inset.java

删除:

package com.c;

import java.io.IOException;
import java.io.PrintWriter;

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 com.a.DBHelper;

@WebServlet("/Delete")
public class Delete extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求和响应的字符集编码
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter(); //获得输出流
        
        String strID = request.getParameter("newsid");
        DBHelper dbhelper=new DBHelper();
        dbhelper.connect();
        if(""!=strID){
            dbhelper.delete(strID);
            out.println("删除成功!请返回");
        }
        
        dbhelper.close();
    }

    /**
         * The doPost method of the servlet. 
* * This method is called when a form has its tag value method equals to post. * *
@param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } Delete.java

查询:

package com.b;

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

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 com.a.DBHelper;
import com.a.NewsInfo;

@WebServlet("/Select")
public class Select extends HttpServlet {

    /**
         * The doGet method of the servlet. 
* * This method is called when a form has its tag value method equals to get. * *
@param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置请求和响应的字符集编码 response.setContentType("text/html; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); //获得输出流 /* //数据库连接 DBHelper dbhelper=new DBHelper(); dbhelper.connect(); //执行6次查询 String[] newstime=new String[]{"imagenews","academicexchange","newsnow","partyactive","notice","spetiallist"}; for(int time=0;time newsall=dbhelper.myselect(); for(int i=0;i*/ for(int q=0;q<6;q++){ if(null!=request.getSession().getAttribute("Imagenews"+q+"ID")){ request.getSession().removeAttribute("Imagenews"+q+"ID"); request.getSession().removeAttribute("Imagenews"+q+"Name"); } } DBHelper dbhelper=new DBHelper(); dbhelper.connect(); String sql="select * from imagenews"; dbhelper.select(sql); List newsall=dbhelper.myselect(); for(int i=0;i){ NewsInfo Imagenews = newsall.get(i); request.getSession().setAttribute("Imagenews"+i+"ID", Imagenews.getId()); request.getSession().setAttribute("Imagenews"+i+"Name", Imagenews.getName()); } dbhelper.close(); response.sendRedirect("index.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } Select.java

 

你可能感兴趣的:(ssm重新开发计科院新闻网站)