java 三大框架共同协作 来动态的显示新闻

语言:java

框架:Spring+Hibernate+Struts2

作用:我做的是展示公司的新闻,实现动态从数据库中显示公司的新闻,浏览者可以看到新闻的详情;

java 三大框架共同协作 来动态的显示新闻_第1张图片


 一、数据库部分

java 三大框架共同协作 来动态的显示新闻_第2张图片

二、实体类和映射文件以及把映射文件放在核心配置文件里面

package cn.com.bean;

public class News {
	//新闻编号、标题、内容、发布新闻的日期
private int newsid;
private String newsimg;
private String title;
private String context;
private String date;
public int getNewsid() {
	return newsid;
}
public void setNewsid(int newsid) {
	this.newsid = newsid;
}
public void setNewsimg(String newsimg) {
	this.newsimg = newsimg;
}
public String getNewsimg() {
	return newsimg;
}
public String getTitle() {
	return title;
}
public void setTitle(String title) {
	this.title = title;
}
public String getContext() {
	return context;
}
public void setContext(String context) {
	this.context = context;
}
public String getDate() {
	return date;
}
public void setDate(String date) {
	this.date = date;
}

}















	
	
	
	
		
		
		
		
	
	
	
	
		
		
						
				true
				true
				update
		     	org.hibernate.dialect.MySQL5InnoDBDialect
			thread    
			
		
		
		                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
		
		
		classpath:cn/com/bean/Goods.hbm.xml
		classpath:cn/com/bean/News.hbm.xml
		
		
	

























查询数据库中的news表中所有的数据以及点击标题或者图片显示新闻的详情内容的类

package cn.com.beans;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.com.bean.News;
import cn.com.inter.NewsService;
public class NewsServiceImpl implements NewsService,ModelDriven,Preparable{
	//显示新闻的页面
	//1.连接数据库
	private SessionFactory sessionfactory;
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}
	public SessionFactory getSessionfactory() {
		return sessionfactory;
	}
	public Session GetSession(){
		return sessionfactory.openSession();
	}
	//1.查询news表中所有的数据
	public List selectNews() {
		Session sesison=GetSession();
		String sql="from News";
		Query query=sesison.createQuery(sql);
	    List list=query.list();	   
		return list;
	}
//从前台获取当前的一条详细信息
	private News news;
	public void setNews(News news) {
		this.news = news;
	}
	public News getNews() {
		return news;
	}
	private int newsid;
	public void setNewsid(int newsid) {
		this.newsid = newsid;
	}
	public int getNewsid() {
		return newsid;
	}
	public String select_onenews(){
		String sql="from News where newsid=?";
		Query query=GetSession().createQuery(sql);
		query.setLong(0, newsid);
		News nn=(News) query.uniqueResult();
	    news=nn; 
	    return "newsinfo";
	}
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("news prepare....");
	}
	@Override
	public News getModel() {
		// TODO Auto-generated method stub
		return news;
	}
	
}

我先简化一下前台的代码

 

首先是一个超链接

新闻中心

 

然后到这个servlet页面

package cn.com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.com.bean.News;
import cn.com.inter.NewsService;
public class NewsInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.创建ioc容器
		ServletContext sc = getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
		NewsService ns = (NewsService) ac.getBean("newsService");
		List list = ns.selectNews();
	   request.setAttribute("list", list);
	   request.getRequestDispatcher("/news.jsp").forward(request, response);
	}

}

 web.xml里面的配置如下(有不知道如何整合三大框架的童鞋们可以参考)



  	
  
    index.jsp
  
  
 
   
   
  contextConfigLocation 
   
  classpath:applicationContext.xml
    
    
    
   org.springframework.web.context.ContextLoaderListener 
   

  
    GoodsServlet
    cn.com.servlet.GoodsServlet
  
  
    NewsInfoServlet
    cn.com.servlet.NewsInfoServlet
  


  
    GoodsServlet
    /GoodsServlet
  
  
    NewsInfoServlet
    /NewsInfoServlet
  
  
    
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    

    
        struts2
        /*
    




后台就结束了

前台显示页面如下  后面附上前台代码,我的前台框架是BootStrap

java 三大框架共同协作 来动态的显示新闻_第3张图片

news.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
        
    力凡公司-新闻页面  
	
	
	    
	
	
    
  
  
 	
	
	
	
	  


详情页面news_info.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
       
    力凡公司-新闻详情页面
	
	
	    
	
	
    
  
 	
	
	

${news.title}

原创 力凡公司 ${news.date }





  ${news.context }

java 三大框架共同协作 来动态的显示新闻_第4张图片


 贴心提示:

如何三大框架还没有整合好的,例如jar包不知道有没有导全的,怎么整合的,看这里

https://blog.csdn.net/qq_37591637/article/details/86011718

再有什么不懂得,可以评论我,我会很快回复的!

你可能感兴趣的:(java之ssh框架专栏)