SSM框架@DateTimeFormat @JsonFormat日期轉換,登錄注冊功能,高效切換文件界面的方法
本期精彩內(nèi)容:
@DateTimeFormat(pattern="yyyy-MM-dd")
? ? @JsonFormat(pattern="yyyy-MM-dd")
@ModelAttribute("SpringWeb")?
高效切換文件界面的方法
Oracle數(shù)據(jù)庫部分
eclipse中的Java等代碼部分
必導入項目的包
applicationContext.xml文件配置事務等的功能
springmvc-servlet.xml設置界面跳轉
訪問路徑
運行效果
注意:
如果不在實體類中加
@DateTimeFormat(pattern="yyyy-MM-dd")
? ? @JsonFormat(pattern="yyyy-MM-dd")
這些注解,注冊功能等時會報400的錯誤(我經(jīng)歷過),加上這些注解后,插入日期格式的字符串到數(shù)據(jù)庫等時會發(fā)生自動轉換數(shù)據(jù)類型,當然了,不一定是我說的這樣,我只是暫時地這么猜想和理解。總之SSM框架中的注解可以做很多事,可以輕松解決很多BUG,報錯等等。
同時@ModelAttribute("SpringWeb") 的話,暫時理解成SSM框架中的固定的格式就可以,其實我在注冊部分的controller的方法中刪除這個部分或修改其里面的字符串內(nèi)容都沒有什么功能等方面的影響,可能是SSM框架自動處理了一些內(nèi)容,代替了@ModelAttribute("SpringWeb") 的作用等等。

高效切換文件界面的方法:

Oracle數(shù)據(jù)庫部分:
?--drop table Userinfo? ? ? ? ? ? ? ??
create table Userinfo(
? ? id number primary key,
? ? act varchar2(30) not null,
? ?pwd varchar2(30) not null,
? ?birth date
);
--drop sequence seq_Userinfo
create sequence seq_Userinfo
start with 1? ? ? ?--起始值是1
increment by 1? ? ?--增長的值? ?
maxvalue 999999999 --序列號的最大值
minvalue 1? ? ? ? ?--序列號的最小值
nocycle? ? ? ? ? ? --是否循環(huán)
cache 10;? ? ? ? ? --預存
insert into Userinfo values(seq_Userinfo.nextval,'黑黑','pwd1',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'紅紅','pwd2',to_date('2020-06-07','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'藍藍','pwd3',to_date('2020-06-08','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'666','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'999','pwd5',to_date('2020-06-10','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'888','pwd6',to_date('2020-06-11','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'三連','pwd5',to_date('2020-06-10','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'關注','pwd6',to_date('2020-06-11','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱1','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱2','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱3','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱4','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱5','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
--select * from Userinfo?

eclipse中的Java等代碼部分:

package com.SSHC.bean;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Userinfo {
? ? private Integer id;
? ? private String act;
? ? private String pwd;
? ? @DateTimeFormat(pattern="yyyy-MM-dd")
? ? @JsonFormat(pattern="yyyy-MM-dd")
? ? private Date birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAct() {
return act;
}
public void setAct(String act) {
this.act = act;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}


package com.SSHC.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.SSHC.bean.Userinfo;
import com.SSHC.service.PublicService;
@Controller
public class LoginController {
@Resource
private PublicService pubService;
@RequestMapping("RequestMappingLogin")
? ? public String doLogin(@ModelAttribute("SpringWeb") Userinfo u
? ? ,HttpSession session,Model m){
? ? u = pubService.login(u);
? ? if(u != null && u.getId() > 0) {
? ? //將u放到session中去
? ? session.setAttribute("_user", u);
? ? ? ? return "loginSuccess";
? ? } else {
? ? m.addAttribute("msg","登錄失敗");
? ? return "login";
? ? }
? ? }
}




package com.SSHC.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.SSHC.bean.Userinfo;
@Repository
public interface UserinfoDao {
? ? List<Userinfo> selectAll();
? ? Userinfo selectByActAndPwd(Userinfo u);
? ? Integer add(Userinfo u);
}


<?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">
<!-- namespace不能亂寫,必須寫成IUserinfoDao接口的全路徑 -->
<mapper namespace="com.SSHC.dao.UserinfoDao">
? ? <resultMap type="Userinfo" id="rmUserinfo">
? ? ? ? <id property="id" column="ID"/>
? ? <result property="act" column="ACT"/>
? ? <result property="pwd" column="PWD"/>
? ? <result property="birth" column="BIRTH"/>
? ? </resultMap>?
? ? <select id="selectAll" resultMap="rmUserinfo">
? ? ? ? select * from userinfo
? ? </select>?
? ? <!-- Userinfo selectByActAndPwd(String act,String pwd) -->
? ? <select id="selectByActAndPwd" resultMap="rmUserinfo"
? ? ? ? parameterType="Userinfo">
? ? ? ? select * from userinfo where act = #{act}?
? ? ? ? and pwd = #{pwd}
? ? </select>
<insert id="add" parameterType="Userinfo">
? ? ? ? insert into Userinfo values?
? ? ? ? (seq_Userinfo.nextval,#{act},#{pwd},#{birth})
? ? </insert>?
</mapper>


package com.SSHC.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.SSHC.bean.Userinfo;
import com.SSHC.dao.UserinfoDao;
@Service
@Transactional
public class PublicService {
//屬性名就是接口名的首字母改成小寫
@Resource
? ? private UserinfoDao userinfoDao;
? ??
? ? //登錄方法
? ? public Userinfo login(Userinfo u){
? ? return userinfoDao.selectByActAndPwd(u);
? ? }
? ??
? ? public Integer register(Userinfo u){
? ? Integer count=userinfoDao.add(u);
? ? return count;
? ? }
}


oracle_drivername=oracle.jdbc.driver.OracleDriver
oracle_url=jdbc:oracle:thin:@localhost:1521:orcl
oracle_username=X
oracle_password=sshcPwd

log4j.rootLogger=DEBUG,Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d[%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

<?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>??
? ? <typeAliases>
? ? ? ? <package name="com.SSHC.bean"/>
? ? </typeAliases>
</configuration>
必導入項目的包:

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:context="http://www.springframework.org/schema/context"
? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? xmlns:tx="http://www.springframework.org/schema/tx"
? ? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
? ? ? ? http://www.springframework.org/schema/tx?
? ? ? ? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
? ? <!-- 配置掃描注解,不掃描帶有@Controller注解的類 -->
? ? <context:component-scan base-package="com.SSHC">
? ? ? ? <context:exclude-filter type="annotation"
? ? ? ? ? ? expression="org.springframework.stereotype.Controller" />
? ? </context:component-scan>
? ? <!-- 引入db.properties文件 -->
? ? <bean id="propertyConfigurer"?
? ? ? ? class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
? ? ? ? <property name="location" value="classpath:db.properties"/>
? ? </bean>
? ? <!--數(shù)據(jù)庫連接池配置-->
? ? <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"?
? ? ? ? destroy-method="close">??
? ? ? ? <property name="driverClassName" value="${oracle_drivername}"/>
? ? ? ? <property name="url" value="${oracle_url}"/>
? ? ? ? <property name="username" value="${oracle_username}"/>
? ? ? ? <property name="password" value="${oracle_password}"/>
? ? </bean>
? ? <!-- 創(chuàng)建sqlSessionFactory對象 -->
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!-- 指定數(shù)據(jù)源 -->
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <!-- 指定mybatis框架主配置文件的位置 -->
? ? ? ? <property name="configLocation" value="classpath:mybatis.xml"/>
? ? ? ? <!-- 自動掃描mapping.xml文件,**表示迭代查找 ,,也可在mybatis.xml中單獨指定xml文件 -->
? ? ? ? <property name="mapperLocations" value="classpath:com/SSHC/dao/*.xml"/>
? ? </bean>?
? ? <!-- 自動掃描com/SSHC/dao下的所有dao接口,并實現(xiàn)這些接口,
? ? ? ? ? ? ? ? ?可直接在程序中使用dao接口,不用再獲取sqlsession對象 -->
? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <!-- basePackage 屬性是映射器接口文件的包路徑。
? ? ? ? ? ? ? ? ? ? ? ? 你可以使用分號或逗號 作為分隔符設置多于一個的包路徑-->
? ? ? ? <property name="basePackage" value="com/SSHC/dao"/>
? ? ? ? <!-- 因為會自動裝配 SqlSessionFactory和SqlSessionTemplate
? ? ? ? ? ? ? ? ? ? ? ? 所以沒有必要去指定SqlSessionFactory或 SqlSessionTemplate
? ? ? ? ? ? ? ? ? ? ? ? 因此可省略不配置;
? ? ? ? ? ? ? ? ? ? ? ? 但是,如果你使用了一個以上的 DataSource,那么自動裝配可能會失效。
? ? ? ? ? ? ? ? ? ? ? ? 這種情況下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName屬性
? ? ? ? ? ? ? ? ? ? ? ? 來設置正確的 bean名稱來使用 -->
? ? ? ? ?<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
? ? </bean>
? ? <!-- 配置事務管理器 -->
? ? <bean id="transactionManager"
? ? ? ? class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? <property name="dataSource" ref="dataSource" />
? ? </bean>
? ? <!--? 使用聲明式事務 transaction-manager:引用上面定義的事務管理器 -->
? ? <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size: 50px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <form action="RequestMappingLogin" method="post">
? ? ? ? ? ? <label>賬號:</label><input type="text" name="act"/>
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>密碼:</label><input type="password" name="pwd"/>
? ? ? ? ? ? <br>
? ? ? ? ? ? <input type="submit" value="登錄" />
? ? ? ? ? ? 還沒有注冊?<a href="http://localhost:8080/SSMhomework2/toRegister">注冊</a>
? ? ? ? </form>
? ? ? ? <div>${msg }</div>
? ? </body>
</html>


<%@page import="com.SSHC.bean.Userinfo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
? ? Userinfo u = (Userinfo)session.getAttribute("_user");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>登錄成功!<%=u.getAct() %></h1>
? <h1><a href="http://localhost:8080/SSMhomework2/toLogin">退出賬號</a>
? </h1>? </body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size: 50px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <form action="RequestMappingRegister" method="post">
? ??
? ? ? ? ? ? <label>賬號:</label><input type="text" name="act"/>
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>密碼:</label><input type="password" name="pwd"/>
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>生日:</label><input type="date" name="birth"/>
? ? ? ? ? ? <br>
? ? ? ? ? ? <input type="submit" value="注冊" />
? ? ? ? ? ? 已經(jīng)注冊?
<a href="http://localhost:8080/SSMhomework2/toLogin">登錄</a>
? ? ? ? </form>
? ? ? ? <div>${msg }</div>
? ? </body>
</html>

springmvc-servlet.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:context="http://www.springframework.org/schema/context"
? ? xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">? ? ? ? ? ? ? ? ? ? ? ?
? ? <!-- 掃描@Controller注解 -->
? ? <context:component-scan base-package="com.SSHC.controller">
? ? ? ? <context:include-filter type="annotation"
? ? ? ? ? ? expression="org.springframework.stereotype.Controller" />
? ? </context:component-scan>
? ? <!-- 默認注冊RequestMappingHandlerMapping和RequestMappingHandlerAdapter類 -->
? ? <mvc:annotation-driven />
? ? <!-- jsp引用外部js,css等靜態(tài)資源的解決方法(和上面的標簽必須同時出現(xiàn),否則無法訪問url) -->
? ? <mvc:default-servlet-handler />
? ? <!-- 配置視圖名稱解析器 -->
? ? <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"?
? ? ? ? ? ? id="internalResourceViewResolver">
? ? ? ? <!-- 前綴 -->
? ? ? ? <!-- 將所有的jsp文件存放在/WEB-INF/my/目錄下 -->
? ? ? ? <property name="prefix" value="/WEB-INF/" />
? ? ? ? <!-- 后綴 -->
? ? ? ? <property name="suffix" value=".jsp" />
? ? ? ? <!-- 優(yōu)先級設定 -->
? ? ? ? <property name="order" value="10"></property>
? ? </bean>?
? ? <!-- 登錄:http://localhost:8080/SSMhomework2/toLogin -->
? ? <mvc:view-controller path="/toLogin" view-name="login"/>
? ? <!-- 注冊:http://localhost:8080/SSMhomework2/toRegister -->
? ? <mvc:view-controller path="/toRegister" view-name="register"/>?
</beans>


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
? <display-name>SSMhomework2</display-name>
? <!-- springcore框架配置 -->
? <listener>
? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>
? <!-- controller中文亂碼處理,注意一點:要配置在所有過濾器的前面 -->
? <filter>
? ? <filter-name>CharacterEncodingFilter</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>
? </filter>
? <filter-mapping>
? ? <filter-name>CharacterEncodingFilter</filter-name>
? ? <url-pattern>/*</url-pattern>
? </filter-mapping>
? <!-- springmvc框架配置 -->
? <servlet>
? ? ? <servlet-name>springmvc</servlet-name>
? ? ? <servlet-class>
? ? ? ? ? org.springframework.web.servlet.DispatcherServlet
? ? ? </servlet-class>
? ? ? <load-on-startup>1</load-on-startup>
? </servlet>
? <servlet-mapping>
? ? ? <servlet-name>springmvc</servlet-name>
? ? ? <url-pattern>/</url-pattern>
? </servlet-mapping>
? <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>
</web-app>

訪問路徑:
登錄:http://localhost:8080/SSMhomework2/toLogin
注冊:http://localhost:8080/SSMhomework2/toRegister
登錄form表單提交:RequestMappingLogin
注冊form表單提交:RequestMappingRegister

運行效果:





