一步一步学习springmvc之三:spring+springmvc+jpa+在线文本编辑

说明:这是一个javaweb实现在线文本编辑demo

架构:spring+springmvc+jpa

1.在此使用的是fckeditor组件版本fckeditor-java-2.6,大家可以在官网上下载,在此需要导入的jar文件有


2.添加css和js文件,在此文件结构如下,该示例中本人将所有的都拷进web工程,实际可以根据需要选择性的拷贝(下载包中自带的)


3.现在来编写博客的在线编辑页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="net.fckeditor.*"%>
<%@ taglib uri="http://java.fckeditor.net" prefix="FCK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>博客内容编辑模块</title>
<meta name="robots" content="noindex, nofollow" />
<link href="../fckeditor/sample.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../fckeditor/fckeditor.gif"
	type="image/x-icon" />
<script type="text/javascript" src="../fckeditor/fckconfig.js"></script>
<script type="text/javascript">
	function FCKeditor_OnComplete(editorInstance) {
		window.status = editorInstance.Description;
	}
</script>
</head>
<%
	FCKeditor fckEditor = new FCKeditor(request, "EditorDefault");
%>
<body>
	<form action="saveBlog.htm" method="post">
		<%
			fckEditor
					.setValue("This is some <strong>sample text</strong>. You are using <a href=\"http://www.fckeditor.net\">FCKeditor</a>.");
			out.println(fckEditor);
		%>
		<input type="submit" value="提交" />
	</form>
</body>
</html>

3.控制器部分代码,在此处省略了业务层(springapp.web.HelloController)

@RequestMapping("**/saveBlog")
	public String saveBlog(Blog blog,HttpServletRequest request){
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("jpa");
		EntityManager manager = emf.createEntityManager();
		String result = "";
		String parameter = "";
		parameter = request.getParameter("EditorDefault");
		System.out.println("parameter="+parameter);
		blog = new Blog();
		blog.setBlogDate(new Date());
		blog.setBlogName("test");
		blog.setBolgContext(parameter);
		manager.getTransaction().begin();
		manager.merge(blog);
		manager.getTransaction().commit();
		System.out.println(manager);
		System.out.println(45);
		return result;
	}
4web.xml配置文件添加以下部分

<!-- 文本编辑器fckeditor配置 -->
	<servlet>
		<servlet-name>ConnectorServlet</servlet-name>
		<servlet-class>
			net.fckeditor.connector.ConnectorServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>ConnectorServlet</servlet-name>
		<!-- Do not wrap this line otherwise Glassfish will fail to load this file -->
		<url-pattern>/fckeditor/editor/filemanager/connectors/*</url-pattern>
	</servlet-mapping>

5.persistence.xml配置文件(位于classpath的META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  <persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="1234"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.format_sql" value="true"/>
      </properties>
  </persistence-unit>
</persistence>

6.编辑页面效果

7.提交后看看数据库

8.然后得到保存的内容并在页面显示,控制器代码部分

@RequestMapping("**/getBlog")
	public String getBlog(HttpServletRequest request){
		String result = "showblog";
		System.out.println("getBlog进");
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("jpa");
		EntityManager manager = emf.createEntityManager();
		Blog blog = null;
		try {
			manager.getTransaction().begin();
			blog = manager.find(Blog.class, 10);//获得id为10 的
			manager.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(blog.getBlogName());
		request.setAttribute("blog", blog);
		return result;
	}

9.展示页面

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>博客展示</title>
<title>FCKeditor - Samples - Posted Data</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex, nofollow" />
<link href="../../fckeditor/sample.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="../../fckeditor/fckeditor.gif"
	type="image/x-icon" />
</head>
<body>
	<table>
		<tr>
			<td nowrap="nowrap"><b>${blog.bolgContext }</b></td>
			<td width="100%">${blog.bolgContext }</td>
		</tr>
	</table>
</body>
</html>
10.请求获得信息

11.实体bean补充

package springapp.entity;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;

@Entity
public class Blog implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	private Integer blogId;
	private String blogName;
	@Lob
	private String bolgContext;
	private Date blogDate;
	public Integer getBlogId() {
		return blogId;
	}
	public void setBlogId(Integer blogId) {
		this.blogId = blogId;
	}
	public String getBlogName() {
		return blogName;
	}
	public void setBlogName(String blogName) {
		this.blogName = blogName;
	}
	public String getBolgContext() {
		return bolgContext;
	}
	public void setBolgContext(String bolgContext) {
		this.bolgContext = bolgContext;
	}
	public Date getBlogDate() {
		return blogDate;
	}
	public void setBlogDate(Date blogDate) {
		this.blogDate = blogDate;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((blogId == null) ? 0 : blogId.hashCode());
		result = prime * result
				+ ((blogName == null) ? 0 : blogName.hashCode());
		result = prime * result
				+ ((bolgContext == null) ? 0 : bolgContext.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Blog other = (Blog) obj;
		if (blogId == null) {
			if (other.blogId != null)
				return false;
		} else if (!blogId.equals(other.blogId))
			return false;
		if (blogName == null) {
			if (other.blogName != null)
				return false;
		} else if (!blogName.equals(other.blogName))
			return false;
		if (bolgContext == null) {
			if (other.bolgContext != null)
				return false;
		} else if (!bolgContext.equals(other.bolgContext))
			return false;
		return true;
	}
	
}


好了,在此完成,若有疑问,欢迎留言!

ps:对于springmvc不了解的可以可我之前写的springmvc(之一,二)部分,此demo用到之前写的内容

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值