ssm框架

  1. dependencies>
  2. <dependency>
  3. <groupId>org.mybatisgroupId>
  4. <artifactId>mybatisartifactId>
  5. <version>3.4.1version>
  6. dependency>
  7. <dependency>
  8. <groupId>mysqlgroupId>
  9. <artifactId>mysql-connector-javaartifactId>
  10. <version>5.1.29version>
  11. dependency>
  12. <dependency>
  13. <groupId>org.slf4jgroupId>
  14. <artifactId>slf4j-log4j12artifactId>
  15. <version>1.6.1version>
  16. dependency>
  17. <dependency>
  18. <groupId>org.slf4jgroupId>
  19. <artifactId>slf4j-apiartifactId>
  20. <version>1.6.1version>
  21. dependency>
  22. <dependency>
  23. <groupId>org.springframeworkgroupId>
  24. <artifactId>spring-jdbcartifactId>
  25. <version>4.3.11.RELEASEversion>
  26. dependency>
  27. <dependency>
  28. <groupId>org.springframeworkgroupId>
  29. <artifactId>spring-aspectsartifactId>
  30. <version>4.3.11.RELEASEversion>
  31. dependency>
  32. <dependency>
  33. <groupId>org.springframeworkgroupId>
  34. <artifactId>spring-contextartifactId>
  35. <version>4.3.11.RELEASEversion>
  36. dependency>
  37. <dependency>
  38. <groupId>org.mybatisgroupId>
  39. <artifactId>mybatis-springartifactId>
  40. <version>1.3.1version>
  41. dependency>
  42. <dependency>
  43. <groupId>org.springframeworkgroupId>
  44. <artifactId>spring-webmvcartifactId>
  45. <version>4.3.11.RELEASEversion>
  46. dependency>
  47. <dependency>
  48. <groupId>jstlgroupId>
  49. <artifactId>jstlartifactId>
  50. <version>1.2version>
  51. dependency>
  52. <dependency>
  53. <groupId>com.github.pagehelpergroupId>
  54. <artifactId>pagehelperartifactId>
  55. <version>4.2.1version>
  56. dependency>
  57. <dependency>
  58. <groupId>com.fasterxml.jackson.coregroupId>
  59. <artifactId>jackson-databindartifactId>
  60. <version>2.7.4version>
  61. dependency>
  62. <dependency>
  63. <groupId>commons-fileuploadgroupId>
  64. <artifactId>commons-fileuploadartifactId>
  65. <version>1.3.3version>
  66. dependency>
  67. dependencies>
  68.  
  69. <build>
  70. <finalName>ssm-blog-adminfinalName>
  71. <resources>
  72. <resource>
  73. <directory>src/main/javadirectory>
  74. <includes>
  75. <include>**/*.xmlinclude>
  76. includes>
  77. <filtering>truefiltering>
  78. resource>
  79. <resource>
  80. <directory>src/main/resourcesdirectory>
  81. <includes>
  82. <include>**/*.xmlinclude>
  83. <include>**/*.propertiesinclude>
  84. includes>
  85. resource>
  86. resources>
  87. build>

实体类pojo

Diary.java

  1. public class Diary {
  2. private Integer id; //主键ID
  3. private Integer userId; //用户ID
  4. private Date createdDate; //日记创建时间
  5. private String content; //日记内容
  6. private String title; //日记标题
  7. private int status; //身份权限,1:有效,0:无效
  8. private String nickname; //昵称
  9. private AdminUser adminUser; //管理员
  10.  
  11. public Integer getId() {
  12. return id;
  13. }
  14.  
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18.  
  19. public Integer getUserId() {
  20. return userId;
  21. }
  22.  
  23. public void setUserId(Integer userId) {
  24. this.userId = userId;
  25. }
  26. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
  27. public Date getCreatedDate() {
  28. return createdDate;
  29. }
  30.  
  31. public void setCreatedDate(Date createdDate) {
  32. this.createdDate = createdDate;
  33. }
  34.  
  35. public String getContent() {
  36. return content;
  37. }
  38.  
  39. public void setContent(String content) {
  40. this.content = content;
  41. }
  42.  
  43. public String getTitle() {
  44. return title;
  45. }
  46.  
  47. public void setTitle(String title) {
  48. this.title = title;
  49. }
  50.  
  51. public int getStatus() {
  52. return status;
  53. }
  54.  
  55. public void setStatus(int status) {
  56. this.status = status;
  57. }
  58.  
  59. public String getNickname() {
  60. return nickname;
  61. }
  62.  
  63. public void setNickname(String nickname) {
  64. this.nickname = nickname;
  65. }
  66.  
  67. public AdminUser getAdminUser() {
  68. return adminUser;
  69. }
  70.  
  71. public void setAdminUser(AdminUser adminUser) {
  72. this.adminUser = adminUser;
  73. }
  74. }


DAO层

DiaryDAO.java

  1. public interface DiaryDAO {
  2. //插入日记方法
  3. void addDiary(Diary diary);
  4. //查询日记列表
  5. List getDiaryList(Diary diary);
  6. //根据id查询日记
  7. Diary getDiaryById(int id);
  8. //更新日记
  9. void updateDiary(Diary diary);
  10. //更新用户权限
  11. void updateDiaryStatus(Diary diary);
  12. }

 

DiaryMapper.xml

  1. mapper
  2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.ssm.blog.dao.DiaryDAO">
  5.  
  6. <insert id="addDiary" parameterType="Diary">
  7. INSERT INTO diary(USER_ID,CREATED_DATE,CONTENT,TITLE,STATUS)
  8. VALUES (#{userId},now(),#{content},#{title},#{status})
  9. insert>
  10.  
  11. <resultMap id="diaryMap" type="Diary">
  12. <id property="id" column="ID"/>
  13. <result property="userId" column="USER_ID"/>
  14. <result property="createdDate" column="CREATED_DATE"/>
  15. <result property="content" column="CONTENT"/>
  16. <result property="title" column="TITLE"/>
  17. <result property="status" column="STATUS"/>
  18. resultMap>
  19. <select id="getDiaryList" parameterType="Diary" resultMap="diaryMap">
  20. SELECT D.ID,D.TITLE,D.USER_ID,D.CREATED_DATE,D.CONTENT,D.STATUS,A.NICKNAME
  21. FROM diary D
  22. INNER JOIN ADMIN_USER A ON D.USER_ID=A.ID
  23. <where>
  24. <if test="id !=null">D.ID=#{id}if>
  25. <if test="title !=null and title!=''">and TITLE like concat('%',#{title},'%')if>
  26. <if test="nickname !=null and nickname!=''">and NICKNAME like concat('%',#{nickname},'%')if>
  27. where>
  28. select>
  29.  
  30. <select id="getDiaryById" parameterType="int" resultMap="diaryMap">
  31. SELECT ID,TITLE,CONTENT,STATUS
  32. FROM Diary
  33. WHERE ID=#{id}
  34. select>
  35.  
  36. <update id="updateDiary" parameterType="Diary">
  37. update diary set TITLE=#{title},CONTENT=#{content},STATUS=#{status}
  38. where ID=#{id}
  39. update>
  40.  
  41. <update id="updateDiaryStatus" parameterType="Diary">
  42. update diary set STATUS=0
  43. where ID=#{id}
  44. update>
  45. mapper>


service层

DiaryService.java

  1. public interface DiaryService {
  2. //插入日记
  3. void addDiary(Diary diary);
  4.  
  5. //查询日记列表并分页
  6. PageInfo getDiaryList(Diary diary,int pageNum,int pageSize);
  7.  
  8. //根据id查询,用于回显
  9. Diary getDiaryById(Integer id);
  10.  
  11. //更新日记
  12. void updateDiary(Diary diary);
  13. //更改权限
  14. void updateDiaryStatus(int id);
  15. }

 

service层  DiarySrviceImpl.java

  1. //加service注解
  2. @Service
  3. public class DiaryServiceImpl implements DiaryService {
  4. // 当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,
  5. // 并自动注入到相应的地方去。
  6. @Autowired
  7. private DiaryDAO diaryDAO;
  8.  
  9. //插入日记
  10. @Override
  11. public void addDiary(Diary diary) {
  12. diaryDAO.addDiary(diary);
  13. }
  14. //查询日记列表并分页
  15. @Override
  16. public PageInfo getDiaryList(Diary diary, int pageNum, int pageSize) {
  17. PageHelper.startPage(pageNum, pageSize);
  18. List diaryList = diaryDAO.getDiaryList(diary);
  19. PageInfo pageInfo = new PageInfo<>(diaryList);
  20. return pageInfo;
  21. }
  22. //根据id查询日记
  23. @Override
  24. public Diary getDiaryById(Integer id) {
  25. return diaryDAO.getDiaryById(id);
  26. }
  27. //更新日记
  28. @Override
  29. public void updateDiary(Diary diary) {
  30. diaryDAO.updateDiary(diary);
  31. }
  32. //更新身份权限
  33. @Override
  34. public void updateDiaryStatus(int id) {
  35. Diary diary = diaryDAO.getDiaryById(id);
  36.  
  37. diaryDAO.updateDiaryStatus(diary);
  38. }
  39. }
  40.  

controller层

 DiaryController.java

  1. //加controller注解,处理由DispatcherServlet分发的请求
  2. @Controller
  3. //定义url
  4. @RequestMapping("/diary.html")
  5. public class DiaryController {
  6. @Autowired
  7. private DiaryService diaryService;
  8.  
  9. @RequestMapping(params = "act=edit")
  10. @ResponseBody //这个注解表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用。
  11. //添加和修改,一个方法实现两个功能
  12. public Map edit(Diary diary){
  13. if (diary.getId() == null) {
  14. //从session中获取user的信息,后面写完登录再改
  15. diary.setUserId( 2);
  16. diaryService.addDiary(diary);
  17. } else{
  18. //修改
  19. diaryService.updateDiary(diary);
  20. }
  21. return JsonUtils.getSuccessMessage("编辑成功",null);
  22. }
  23.  
  24. //查询日记列表并分页
  25. @RequestMapping(params = "act=list")
  26. @ResponseBody
  27. public Map list(Diary diary, int page, int rows){
  28. PageInfo pageInfo = diaryService.getDiaryList(diary, page, rows);
  29. return JsonUtils.getDatagridPagerResult(pageInfo.getTotal(),pageInfo.getList());
  30. }
  31.  
  32. //根据id查询所有,用于点击修改后回显
  33. @RequestMapping(params = "act=get")
  34. public ModelAndView getDiary(Integer id){
  35. Diary diary = null;
  36. if (id!=null){
  37. diary = diaryService.getDiaryById(id);
  38. }
  39. return new ModelAndView("diary_edit","diary",diary);
  40. }
  41.  
  42. //文件上传
  43. @Value("${DIARY_IMAGE_DIR}")
  44. private String DIARY_IMAGE_DIR;
  45. @Value("${DIARY_IMAGE_URL}")
  46. private String DIARY_IMAGE_URL;
  47.  
  48. @RequestMapping(params = "act=upload")
  49. @ResponseBody
  50. public String upload(MultipartFile file) {
  51. return JsonUtils.fileUpload(file,DIARY_IMAGE_DIR,DIARY_IMAGE_URL);
  52. }
  53.  
  54. //删除(修改权限)
  55. @RequestMapping(params = "act=delete")
  56. @ResponseBody
  57. public Map delete(int id){
  58. Diary diary = diaryService.getDiaryById(id);
  59. if (diary.getStatus()!=0){
  60. diaryService.updateDiaryStatus(id);
  61. }
  62. return JsonUtils.getSuccessMessage("删除权限成功",null);
  63. }
  64. }


util工具类

JsonUtils.java

  1. public class JsonUtils {
  2. public static Map getSuccessMessage(String message,Object other){
  3. Map result = new HashMap<>();
  4. result.put( "status", true);
  5. result.put( "message", "添加成功");
  6. result.put( "other",other);
  7. return result;
  8. }
  9.  
  10. public static Map getErrorMessage(String message,Object other){
  11. Map result = new HashMap<>();
  12. result.put( "status", false);
  13. result.put( "message", "添加成功");
  14. result.put( "other",other);
  15. return result;
  16. }
  17.  
  18. public static Map getDatagridPagerResult(long total, List list){
  19. Map result = new HashMap<>();
  20. result.put( "total",total); //总条数
  21. result.put( "rows",list); //返回的list数据
  22. return result;
  23. }
  24.  
  25. public static String fileUpload(MultipartFile file,String DIR,String URL){
  26. String oldFileName = file.getOriginalFilename();
  27. String extName = oldFileName.substring(oldFileName.lastIndexOf( "."));
  28. String newFileName = System.currentTimeMillis()+extName;
  29. try {
  30. FileUtils.copyInputStreamToFile(file.getInputStream(), new File(DIR,newFileName));
  31. return "{\"error\":0,\"url\":\"" + URL + newFileName + "\"}";
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. return "{\"error\":1}";
  35. }
  36. }
  37. }


resource

db.properties:配置mysql

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/personal_blog?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=123456

 

log4j.properties:输出日志信息

  1. log4j.rootLogger=DEBUG, stdout
  2.  
  3. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
  6.  
  7. #begin
  8. #for normal test,delete when online
  9. log4j.logger.com.ibatis=DEBUG
  10. log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
  11. log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
  12. log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
  13.  
  14. log4j.logger.java.sql.Connection=DEBUG
  15. log4j.logger.java.sql.Statement=DEBUG
  16. log4j.logger.java.sql.PreparedStatement=DEBUG
  17. log4j.logger.java.sql.ResultSet=DEBUG

 

resourse.properties:配置文件上传路径和url地址

  1. DIARY_IMAGE_DIR=E:/upload/diary/
  2.  
  3. DIARY_IMAGE_URL=http://localhost:8080/upload/diary/


spring-config.xml

  1. <context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true"/>
  2. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  3. <property name="driverClassName" value="${jdbc.driver}"/>
  4. <property name="url" value="${jdbc.url}"/>
  5. <property name="username" value="${jdbc.username}"/>
  6. <property name="password" value="${jdbc.password}"/>
  7. bean>
  8.  
  9. <context:component-scan base-package="com.ssm.blog.*">
  10. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  11. context:component-scan>
  12.  
  13. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  14. <property name="dataSource" ref="dataSource"/>
  15. <property name="mapperLocations" value="classpath:com/ssm/blog/dao/mapper/*.xml"/>
  16. <property name="typeAliasesPackage" value="com.ssm.blog.pojo"/>
  17. <property name="plugins">
  18. <array>
  19. <bean class="com.github.pagehelper.PageHelper">
  20. <property name="properties">
  21. <value>
  22. dialect=mysql
  23. reasonable=true
  24. value>
  25. property>
  26. bean>
  27. array>
  28. property>
  29. bean>
  30. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="basePackage" value="com.ssm.blog.dao"/>
  32. bean>
  33. <bean id="transactionManager"
  34. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  35. <property name="dataSource" ref="dataSource"/>
  36. bean>
  37. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  38. <tx:attributes>
  39. <tx:method name="add*" rollback-for="Exception"/>
  40. <tx:method name="delete*" rollback-for="Exception"/>
  41. <tx:method name="update*" rollback-for="Exception"/>
  42. <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
  43. <tx:method name="do*" rollback-for="Exception"/>
  44. tx:attributes>
  45. tx:advice>
  46. <aop:config>
  47. <aop:pointcut id="serviceMethod"
  48. expression="execution(* com.ssm.blog.service..*(..))"/>
  49. <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
  50. aop:config>

 

WEB-INF

springMVC-servlet.xml

  1. <mvc:annotation-driven content-negotiation-manager="contentNegotiationManagerFactoryBean"/>
  2. <context:component-scan base-package="com.ssm.blog.controller">
  3. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  4. context:component-scan>
  5. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  6. <property name="prefix" value="/">property>
  7. <property name="suffix" value=".jsp">property>
  8. bean>
  9. <mvc:resources mapping="/media/**" location="/media/"/>
  10. <bean id="contentNegotiationManagerFactoryBean"
  11. class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  12. <property name="favorPathExtension" value="false"/>
  13. <property name="favorParameter" value="false"/>
  14. <property name="ignoreAcceptHeader" value="false"/>
  15. <property name="mediaTypes">
  16. <map>
  17. <entry key="json" value="application/json"/>
  18. map>
  19. property>
  20. bean>
  21. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  22. <property name="maxUploadSize" value="1048576"/>
  23. <property name="defaultEncoding" value="UTF-8"/>
  24. <property name="resolveLazily" value="true"/>
  25. bean>


web.xml

  1. <context-param>
  2. <param-name>contextConfigLocationparam-name>
  3. <param-value>classpath:spring-config.xmlparam-value>
  4. context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  7. listener>
  8. <filter>
  9. <filter-name>CharacterEncodingFilterfilter-name>
  10. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  11. <init-param>
  12. <param-name>encodingparam-name>
  13. <param-value>UTF-8param-value>
  14. init-param>
  15. <init-param>
  16. <param-name>forceEncodingparam-name>
  17. <param-value>trueparam-value>
  18. init-param>
  19. filter>
  20. <filter-mapping>
  21. <filter-name>CharacterEncodingFilterfilter-name>
  22. <url-pattern>/*url-pattern>
  23. filter-mapping>
  24. <servlet>
  25. <servlet-name>springMVCservlet-name>
  26. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  27. servlet>
  28. <servlet-mapping>
  29. <servlet-name>springMVCservlet-name>
  30. <url-pattern>*.htmlurl-pattern>
  31. servlet-mapping>
  32. <welcome-file-list>
  33. <welcome-file>/blog/list.htmlwelcome-file>
  34. welcome-file-list>


diary_list.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%--panel面板可以设置整体的高度--%>
  3. <div class="easyui-panel" style="height: 95%">
  4. <table id="diary-datagrid" style="height:450px">
  5. table>
  6. <div id="diary-datagrid-toolbar">
  7. <div>
  8. ID: <input type="text" class="easyui-numberbox" id="search-diary-id"/>
  9. 标题: <input type="text" id="search-diary-title"/>
  10. 昵称: <input type="text" id="search-diary-nickname"/>
  11. <a href="#" class="easyui-linkbutton" iconCls="icon-search"
  12. ο nclick="doDiarySearch()">搜索a>
  13. div>
  14. <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" οnclick="doEditDiary(0)">新增a>
  15. <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" οnclick="doEditDiary(1)">修改a>
  16. <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" οnclick="doDeleteDiary()">删除权限a>
  17. div>
  18. div>
  19.  
  20. <div class="easyui-dialog" closed="true" id="edit-diary-dialog"
  21. modal="true" title="编辑日记" style="width:800px;height:600px;padding:30px" buttons="#dlg-diary-buttons">
  22. div>
  23. <div id="dlg-diary-buttons">
  24. <a href="javascript:void(0)" class="easyui-linkbutton" οnclick="submitDiary()">保存a>
  25. <a href="javascript:void(0)" class="easyui-linkbutton" οnclick="$('#edit-diary-form').form('reset');">重填a>
  26. div>
  27. <link href="<%=request.getContextPath()%>/media/kindeditor-4.1.10/themes/default/default.css" type="text/css"
  28. rel="stylesheet">
  29. <script type="text/javascript" charset="utf-8"
  30. src="<%=request.getContextPath()%>/media/kindeditor-4.1.10/kindeditor-all.js">script>
  31. <script type="text/javascript" charset="utf-8"
  32. src="<%=request.getContextPath()%>/media/kindeditor-4.1.10/lang/zh_CN.js">script>
  33.  
  34. <script type="text/javascript">
  35. $( function () {
  36. $( "#diary-datagrid").datagrid({
  37. url: "<%=request.getContextPath()%>/diary.html?act=list",
  38. pagination: true,
  39. pageList: [5, 10, 15, 20],
  40. toolbar: "#diary-datagrid-toolbar",
  41. frozen: true,//冻结标题行
  42. singleSelect:true,
  43. fit: true,//让表格自动适应面板的高度
  44. nowrap: false,//高度自适应,当内容比较多的时候会自动换行
  45. columns: [[
  46. { field: 'id', title: 'ID', width: 80},
  47. { field: 'title', title: '标题', width: 80},
  48. { field: 'createdDate', title: '发布时间', width: 160},
  49. { field: 'nickname', title: '作者', width: 80},
  50. {
  51. field: 'status', title: '身份权限', width: 80, formatter: function (val, row) {
  52. if (row.status == 0) return "无效";
  53. return row.status
  54. }
  55. }
  56. ]]
  57. })
  58. })
  59.  
  60. function doDiarySearch() {
  61. var queryParams = $('#diary-datagrid').datagrid('options').queryParams;
  62. queryParams.id = $( "#search-diary-id").val();
  63. queryParams.title = $( "#search-diary-title").val();
  64. queryParams.nickname = $( "#search-diary-nickname").val();
  65. //重新加载datagrid
  66. $( "#diary-datagrid").datagrid('reload');
  67. }
  68.  
  69. var kingEditorParams = {
  70. filePostName: "file",//指定上传文件参数名称
  71. uploadJson: '<%=request.getContextPath()%>/diary.html?act=upload',//指定上传文件请求的url。
  72. dir: "image"//上传类型,分别为image、flash、media、file,
  73. }
  74. var editor;
  75.  
  76. function doEditDiary(type) {
  77. var id = "";
  78. var row = $('#diary-datagrid').datagrid('getSelected'); //单选
  79. if (type == 1) {//要修改
  80. if (row == null) {
  81. alert( "请选择要编辑的数据");
  82. return;
  83. }
  84. id = row.id;
  85. }
  86. $( "#edit-diary-dialog").dialog({
  87. href: "<%=request.getContextPath()%>/diary.html?act=get&id=" + id,
  88. onLoad: function () {
  89. editor = KindEditor.create($( "#editor"), kingEditorParams);
  90. },
  91. onBeforeDestroy: function () {
  92. KindEditor.remove( "#editor");
  93. }
  94. }).dialog( 'open');
  95. }
  96.  
  97. function submitDiary() {
  98. $( '#edit-diary-form').form('submit', {
  99. onSubmit: function () {
  100. if ($(this).form('enableValidation').form('validate')) {
  101. editor.sync(); //同步富文本编辑器,否则拿不到
  102. $.ajax({
  103. url: "<%=request.getContextPath()%>/diary.html?act=edit",
  104. data: $("#edit-diary-form").serialize(),
  105. method: "post",
  106. success: function (data) {
  107. if (data.status) {
  108. $( "#diary-datagrid").datagrid('reload');
  109. $( "#edit-diary-dialog").dialog("close");
  110. } else {
  111. alert(data.message)
  112. }
  113. }
  114. })
  115. }
  116. return false;
  117. }
  118. });
  119. }
  120.  
  121. function doDeleteDiary() {
  122. var row = $('#diary-datagrid').datagrid('getSelected');
  123. if (row == null){
  124. return;
  125. }
  126. if (confirm("确定要删除权限吗?")){
  127. $.ajax({
  128. url:"<%=request.getContextPath()%>/diary.html?act=delete",
  129. data:"id=" + row.id,
  130. method:"post",
  131. success:function (data) {
  132. if (data.status){
  133. $( "#diary-datagrid").datagrid('reload');
  134. } else{
  135. alert(data.message)
  136. }
  137. }
  138. })
  139. }
  140. }
  141.  
  142. script>


diary_edit.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--panel面板可以设置整体的高度--%>
    3. <div class="easyui-panel" style="height: 95%">
    4. <table id="diary-datagrid" style="height:450px">
    5. table>
    6. <div id="diary-datagrid-toolbar">
    7. <div>
    8. ID: <input type="text" class="easyui-numberbox" id="search-diary-id"/>
    9. 标题: <input type="text" id="search-diary-title"/>
    10. 昵称: <input type="text" id="search-diary-nickname"/>
    11. <a href="#" class="easyui-linkbutton" iconCls="icon-search"
    12. ο nclick="doDiarySearch()">搜索a>
    13. div>
    14. <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" οnclick="doEditDiary(0)">新增a>
    15. <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" οnclick="doEditDiary(1)">修改a>
    16. <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" οnclick="doDeleteDiary()">删除权限a>
    17. div>
    18. div>
    19.  
    20. <div class="easyui-dialog" closed="true" id="edit-diary-dialog"
    21. modal="true" title="编辑日记" style="width:800px;height:600px;padding:30px" buttons="#dlg-diary-buttons">
    22. div>
    23. <div id="dlg-diary-buttons">
    24. <a href="javascript:void(0)" class="easyui-linkbutton" οnclick="submitDiary()">保存a>
    25. <a href="javascript:void(0)" class="easyui-linkbutton" οnclick="$('#edit-diary-form').form('reset');">重填a>
    26. div>
    27. <link href="<%=request.getContextPath()%>/media/kindeditor-4.1.10/themes/default/default.css" type="text/css"
    28. rel="stylesheet">
    29. <script type="text/javascript" charset="utf-8"
    30. src="<%=request.getContextPath()%>/media/kindeditor-4.1.10/kindeditor-all.js">script>
    31. <script type="text/javascript" charset="utf-8"
    32. src="<%=request.getContextPath()%>/media/kindeditor-4.1.10/lang/zh_CN.js">script>
    33.  
    34. <script type="text/javascript">
    35. $( function () {
    36. $( "#diary-datagrid").datagrid({
    37. url: "<%=request.getContextPath()%>/diary.html?act=list",
    38. pagination: true,
    39. pageList: [5, 10, 15, 20],
    40. toolbar: "#diary-datagrid-toolbar",
    41. frozen: true,//冻结标题行
    42. singleSelect:true,
    43. fit: true,//让表格自动适应面板的高度
    44. nowrap: false,//高度自适应,当内容比较多的时候会自动换行
    45. columns: [[
    46. { field: 'id', title: 'ID', width: 80},
    47. { field: 'title', title: '标题', width: 80},
    48. { field: 'createdDate', title: '发布时间', width: 160},
    49. { field: 'nickname', title: '作者', width: 80},
    50. {
    51. field: 'status', title: '身份权限', width: 80, formatter: function (val, row) {
    52. if (row.status == 0) return "无效";
    53. return row.status
    54. }
    55. }
    56. ]]
    57. })
    58. })
    59.  
    60. function doDiarySearch() {
    61. var queryParams = $('#diary-datagrid').datagrid('options').queryParams;
    62. queryParams.id = $( "#search-diary-id").val();
    63. queryParams.title = $( "#search-diary-title").val();
    64. queryParams.nickname = $( "#search-diary-nickname").val();
    65. //重新加载datagrid
    66. $( "#diary-datagrid").datagrid('reload');
    67. }
    68.  
    69. var kingEditorParams = {
    70. filePostName: "file",//指定上传文件参数名称
    71. uploadJson: '<%=request.getContextPath()%>/diary.html?act=upload',//指定上传文件请求的url。
    72. dir: "image"//上传类型,分别为image、flash、media、file,
    73. }
    74. var editor;
    75.  
    76. function doEditDiary(type) {
    77. var id = "";
    78. var row = $('#diary-datagrid').datagrid('getSelected'); //单选
    79. if (type == 1) {//要修改
    80. if (row == null) {
    81. alert( "请选择要编辑的数据");
    82. return;
    83. }
    84. id = row.id;
    85. }
    86. $( "#edit-diary-dialog").dialog({
    87. href: "<%=request.getContextPath()%>/diary.html?act=get&id=" + id,
    88. onLoad: function () {
    89. editor = KindEditor.create($( "#editor"), kingEditorParams);
    90. },
    91. onBeforeDestroy: function () {
    92. KindEditor.remove( "#editor");
    93. }
    94. }).dialog( 'open');
    95. }
    96.  
    97. function submitDiary() {
    98. $( '#edit-diary-form').form('submit', {
    99. onSubmit: function () {
    100. if ($(this).form('enableValidation').form('validate')) {
    101. editor.sync(); //同步富文本编辑器,否则拿不到
    102. $.ajax({
    103. url: "<%=request.getContextPath()%>/diary.html?act=edit",
    104. data: $("#edit-diary-form").serialize(),
    105. method: "post",
    106. success: function (data) {
    107. if (data.status) {
    108. $( "#diary-datagrid").datagrid('reload');
    109. $( "#edit-diary-dialog").dialog("close");
    110. } else {
    111. alert(data.message)
    112. }
    113. }
    114. })
    115. }
    116. return false;
    117. }
    118. });
    119. }
    120.  
    121. function doDeleteDiary() {
    122. var row = $('#diary-datagrid').datagrid('getSelected');
    123. if (row == null){
    124. return;
    125. }
    126. if (confirm("确定要删除权限吗?")){
    127. $.ajax({
    128. url:"<%=request.getContextPath()%>/diary.html?act=delete",
    129. data:"id=" + row.id,
    130. method:"post",
    131. success:function (data) {
    132. if (data.status){
    133. $( "#diary-datagrid").datagrid('reload');
    134. } else{
    135. alert(data.message)
    136. }
    137. }
    138. })
    139. }
    140. }
    141.  
    142. script>

你可能感兴趣的:(ssm框架)