一些常用的写法

常用固定写法

引入jstl标签写法,注意前提是已经导入了两个相关jar包
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
找到根目录
${pageContext.request.contextPath }
设置响应字符编码
resp.setContentType("text/html;charset=utf-8");
单元格间距
<table border="1" cellspacing="0">
设置编码
 <meta charset="UTF-8">
<!-- 动态方式就是动态选择下拉框 -->
						<c:forEach items="${categoryList }" var="category">
						<c:choose>
						<c:when test="${category.parentId==content.categoryId }">
						<option value="${category.id }" selected="selected">${category.name }</option>
						</c:when>
						<c:otherwise>
						<option value="${category.id }">${category.name }</option>
						</c:otherwise>
						</c:choose>
						</c:forEach>
jstl标签转义超链接(${content.url }代表数据库存储的地址)
<c:out escapeXml="false" value="<a href='${content.url }'>${content.url }</a>"></c:out>
 清除主键ID的排序,达到从1开始 TRUNCATE TABLE ebook_entry
 无刷新头部
 <%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
点击按钮跳转页面,注意在form表单写法可能不一样
<button onclick="window.location='add.jsp'" >添加书籍</button>
<button onClick="location.href='${pageContext.request.contextPath}/index.jsp'" >返回</button>
from表单中返回或者跳转
<button type="reset" onClick="location.href='${pageContext.request.contextPath}/index.jsp'" >返回</button>
内部样式,应急使用
<tr height="50px" style="background: RGB(201,247,208)">
				<th colspan="2" style="font-size:  40px;font-family: '宋体'  ">增加电子文档</th>
			</tr>

提示prompt,常用确认询问confirm
spring表单标签
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="fm" %>   
rest风格view/{id}
@PathVariable String id
input非空验证
required oninvalid="setCustomValidity('文档名称不能为空')" oninput="setCustomValidity('')"
刷新方式1强制性
window.location.reload();//刷新浏览器界面(不友好)
eclipse报SSL红色jdbc:mysql://localhost:3306/somebodyblog?characterEncoding=utf8&amp;useSSL=false
jstl标签转义超链接(${content.url }代表数据库存储的地址)
<c:out escapeXml="false" value="<a href='${content.url }'>${content.url }</a>"></c:out>
非空验证
import org.apache.commons.lang.StringUtils;
	if(!StringUtils.isNotBlank(name)){
			resultMap.put("name", "existno");//不能为空
		}
清除SVN账户信息
C:\Users\wozniak\AppData\Roaming\Subversion\auth
查看日志
tail -f logs/catalina.out

对请求路径404说再见

//设置根路径,以防多次跳转导致404
var strFullPath = window.document.location.href;
var strPath = window.document.location.pathname;
var pos = strFullPath.indexOf(strPath);
var prePath = strFullPath.substring(0, pos);
var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var basePath = prePath;
var rootPath = prePath + postPath;
上面写成个js,后面引入这个js即可,url:rootPath+'/dosomething.do'

 日期转换

1数据库的datatime类型转换成jsp日常格式,如2018-08-09 21:09:34
2spring-servlet.xml配置json转换器
<mvc:annotation-driven>
		<mvc:message-converters>
			<!-- json格式转换器 -->
			<bean
				class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<!-- 转换的格式 ,设置响应的格式 -->
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=UTF-8</value>
						<value>application/json</value>
					</list>
				</property>
				<!-- 时间格式转换器 -->
				<property name="features">
				<list>
				<value>WriteDateUseDateFormat</value>
				</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

3使用注解写格式 @JSONField(format="yyyy-MM-dd HH:mm:ss")
4实体类导包注意:import java.sql.Date;
----自定义格式---
在页面引入:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
然后在你想要显示的地方设置:
<td><fmt:formatDate value='${obj.createDate}' pattern='yyyy-MM-dd HH:mm:ss'/></td>
实体类加注解(适合实体类是java.util.Date类型的)
@DateTimeFormat(pattern = "yyyy-MM-dd")  
@JSONField(format="yyyy-MM-dd")//JSON转换(可以自定义,默认显示到秒,就是数据库存储的数据类型)
private Date birthday;
private Date receiveAppTime; 

最基本的web.xml(现在用得少了)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>XXXXX项目名称===================</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置字符拦截器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>
	        org.springframework.web.filter.CharacterEncodingFilter
	    </filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
</web-app>

简单table格式 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<table border="1" cellspacing="0">
		<tbody id="">
			<tr>
				<th>用户名</th>
				<th>年龄</th>
				<th>性别</th>
				<th>地址</th>
				<th>用户角色</th>
				<th>图片</th>
				<th>操作</th>
			</tr>
			<c:forEach items="${ }" var="">
				<tr>
					<td>${}</td>
					<td>${}</td>
					<td>${}</td>
					<td>${}</td>
					<td>${}</td>
					<td><img alt="" src="${user.picturePath} "></td>
					<td><a class="deleteUser" href="deleteUser.do?id=${user.id }">删除&nbsp;&nbsp;</a>
						<a href="goupdate.do?id=${user.id }">修改&nbsp;&nbsp;</a><a
						href="viewUser.do?id=${user.id }">查看</a></td>
				</tr>
			</c:forEach>
		</tbody>
	</table>

mybatis-config.xml的基本使用(也不常用了)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<!-- 1引入 database.properties 文件-->
<properties resource="database.properties"/>

<!-- 2配置mybatis的log实现为LOG4J -->
<settings>
		<setting name="logImpl" value="LOG4J" />
	</settings>
	
<!--3:类型别名,下面这样写就不用繁琐命别名了  -->
	<typeAliases>
<!-- <typeAlias alias="provider" type="com.smbms.pojo.Provider"/> -->
	 	<package name="com.xxx.pojo"/>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
<!--4配置事务管理,采用JDBC的事务管理和type="POOLED(mybatis自带的数据源)"  -->
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${user}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<!-- 5将mapper文件加入到配置文件中 -->
	<mappers>
		<mapper resource="com/smbms/dao/首字母小写类名/首字母大写类名Mapper.xml" />
	</mappers>
</configuration>

 Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--确认在核心配置文件已经加载了映射器这里的SQL语句才能执行  -->
<mapper namespace="com.smbms.dao.首字母小写类名.首字母大写类名Mapper">
<!--写SQL语句  -->


</mapper>

 applicationContext.xml(不常用了)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">


</beans>

 log4j.properties

### 设置Logger输出级别(debug优先使用,可以记录很多级别)和目的地###
###有fatal(致命错误)error(错误)warm(警告)info(运行信息)debug(帮助调试)可选
log4j.rootLogger=info, stout,logfile

### 输出信息到控制抬,默认是System.out就相当于System.out.println() ###
log4j.appender.stout=org.apache.log4j.ConsoleAppender
log4j.appender.stout.Target=System.err
log4j.appender.stout.layout=org.apache.log4j.SimpleLayout

### 输出DEBUG 级别以上的日志到D://Aworkspace//log4j/mylog(建议改成项目名称).log
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=D://Aworkspace//log4j/MYSSM.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%l %F %p %m%n

maven配置JDK8

<profile>    
    <id>jdk-1.8</id>    
     <activation>    
          <activeByDefault>true</activeByDefault>    
          <jdk>1.8</jdk>    
      </activation>    
<properties>    
<maven.compiler.source>1.8</maven.compiler.source>    
<maven.compiler.target>1.8</maven.compiler.target>    
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
</properties>    
</profile>
发布了57 篇原创文章 · 获赞 33 · 访问量 814万+

猜你喜欢

转载自blog.csdn.net/wozniakzhang/article/details/86350877