1.${pagecontext.request.contextpath}作用
${pageContext.request.contextPath}等价于<%=request.getContextPath()%>
或者可以说是<%=request.getContextPath()%>的EL版
意思就是取出部署的应用程序名或者是当前的项目名称
比如我的项目名称是quickstart在浏览器中输入为http://localhost:8080/quickstart/register.jsp
${pageContext.request.contextPath}或<%=request.getContextPath()%>取出来的就是/quickstart,而"/"代表的含义就是http://localhost:8080
所以我们项目中应该这样写${pageContext.request.contextPath}/register.jsp
${pageContext.request.contextPath}用于解决使用相对路径时出现的问题,它的作用是取出所部署项目的名字。
2.输出乱码(在页面内上显示信息)
例如
result="<font color='red'>该用户名已经被使用</font>"
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");//response.setContentType("text/html;charset=UTF-8")
response.getWriter().print(result);
3.当前台数据获取的时间为String类型需要转化为Date类型插入到数据库时
(局部转换)
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
//转换日期
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
}
(全局转换)
1.创建CustomDate类实现WebBindingInitializer
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
public class CustomDate implements WebBindingInitializer{
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
// TODO Auto-generated method stub
//转换日期
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
2.在Spring-MVC.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="com.gwd.shopping" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 日期转换 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.gwd.shopping.core.web.CustomDate"/>
</property>
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/back_page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4.Date日期的相加减
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("今天的日期:"+df.format(d));
System.out.println("两天前的日期:" + df.format(new Date(d.getTime() - (long)2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + (long)3 * 24 * 60 * 60 * 1000)));
获取某天的年月日:
int year =calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day =calendar.get(Calendar.DAY_OF_MONTH);
int hour =calendar.get(Calendar.HOUR_OF_DAY);
int minute =calendar.get(Calendar.MINUTE);
int seconds =calendar.get(Calendar.SECOND);
5.复合主键注意在映射hibernate时,注意无论只进行查询操作,都需要配好复合主键,否则查询就会出错。
6.在controller传给前台的list中,注意加上前缀,否则可能得不到其值。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
7.利用Session在两个Controller中传递
@Controller
public class ManagerController {
@Resource
private ManagerService managerServiceImpl;
@RequestMapping(value = "manager/login.do",method = RequestMethod.GET)
public ModelAndView login(ManagerModel managerModel,HttpSession httpSession){
ManagerModel manager = managerServiceImpl.getManager(managerModel);
if(manager!=null){
manager.setPassword("");
httpSession.setAttribute("manager", manager);
return new ModelAndView(new RedirectView("../admin/main.jsp"));
}else{
return new ModelAndView(new RedirectView("../admin/login.jsp"));
}
}
@RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)
public String logout(HttpSession httpSession){
httpSession.getAttribute("manager");
return "success";
}
}
8.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName">
Sep 8, 2018 12:00:00 AM