c语言sscanf函数的用法是什么
286
2023-04-15
SSM框架下实现登录注册的示例代码
基本配置:jdk1.8 tomcat 8 MyEclipse
先打好地基:
spring配置文件 application.xml:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation=" http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd"> class="org.springframework.jdbc.datasource.DriverManagerDataSource">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
springMVC配置文件 :
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd"> class="org.springframework.web.servlet.view.InternalResourceViewResolver"> value="org.springframework.web.servlet.view.JstlView" />
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> value="org.springframework.web.servlet.view.JstlView" />
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> value="org.springframework.web.servlet.view.JstlView" />
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
value="org.springframework.web.servlet.view.JstlView" />
value="org.springframework.web.servlet.view.JstlView" />
web.xml 配置:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="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_2_5.xsd" version="2.5">
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="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_2_5.xsd" version="2.5">
开始第一层啦:
pojo包:UserInfo 类
package pojo;
public class UserInfo {
private String uid;
private String name;
private String email;
private String password;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo [uid=" + uid + ", name=" + name + ", email="
+ email + ", password=" + password + "]";
}
}
mapper层:(注意mybatis的xml文件也要放在mapper层)
ShopMapping.java:
其中@Param注解 是为了和xml中的查询参数进行绑定
package mapper;
import org.apache.ibatis.annotations.Param;
import pojo.UserInfo;
public interface ShopMapper {
public void register(@Param("name")String name,@Param("email")String email,@Param("password")String password);
public UserInfo login(@Param("email")String email,@Param("password")String password);
public int findUser(@Param("email")String email);
}
Shop.xml
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from UserInfo where email=#{email} and password=#{password}
insert into UserInfo(name,email,password) values (#{name},#{email},#{password})
select count(*) from UserInfo where email=#{email}
service层:其实在写登陆的时候用了int类型,在想登陆也只要在数据库中查询表单输入的数据就行了,在mapper层的xml的文件中也写了 select count(*) 查询个数, 但是结果并不好,因为我要做的还有设置session。
package service;
import pojo.UserInfo;
public interface ShopService {
//用户注册
void regist(String name,String email,String password);
//用户登录
UserInfo login(String email,String password);
//验证
int findUser(String email);
}
service实现层:service.Impl
package service.Impl;
import mapper.ShopMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.UserInfo;
import service.ShopService;
@Service
public class ShopServiceImpl implements ShopService {
@Autowired
public ShopMapper sm;
@Override
public void regist(String name, String email, String password) {
sm.register(name, email, password);
}
@Override
public UserInfo login(String email, String password) {
UserInfo user=sm.login(email, password);
if(user!=null &&user.getPassword().equals(password)){
return user;
}
return null;
}
@Override
public int findUser(String email) {
if(sm.findUser(email)==0){
return 0;
}
return 1;
}
}
controller层:
package controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pojo.UserInfo;
import service.ShopService;
@Controller
@RequestMapping("")
public class ShopController {
@Autowired
public ShopService ss;
@RequestMapping(value = "registerUser", method = RequestMethod.POST)
public String registerUser(String name, String email, String password) {
int findUser = ss.findUser(email);
if (findUser == 0) {
ss.regist(name, email, password);
// System.out.println("可以注册");
return "login";
} else {
// System.out.println("注册失败");
return "register";
}
}
@RequestMapping(value = "loginUser", method = RequestMethod.POST)
public String loginUser(UserInfo user, HttpSession session) {
// 调用service方法
user = ss.login(user.getEmail(), user.getPassword());
if (user != null) {
session.setAttribute("u".user);
return "index";
}
return "login";
}
@RequestMapping("/outLogin")
public String outLogin(HttpSession session){
session.invalidate();
return "index";
}
}
在controller层当中,关于注册的格式要求还需要自行搜索一下,主要讲一下的是登陆。在登陆的这个方法中传递了两个形式参数,UserInfo是实体类,HttpSssion是设置session的关键,后面通过session.setAttribute()设置session,这也是在上文中提到的需要session的部分。在后来的注销中可以使用session.invalidate。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~