Linux中怎么用cat命令创建文件并写入数据
226
2023-07-14
基于SSM框架实现简单的登录注册的示例代码
一、环境配置工程目录
在pom.xml添加依赖
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
数据库驱动配置
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysystem?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=kongtao
spring-web.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
二、具体逻辑实现数据访问对象(DAO接口)
UserDao.java
package com.sy.dao;
import org.apache.ibatis.annotations.Param;
import com.sy.entity.User;
public interface UserDao {
/**
* 查找用户名和密码
* @param username 登录用户名
* @return
*/
User findByUsername(String username);
/**
* 注册用户和密码
*/
void registerByUsernameAndPassword(@Param("username")String username,
@Param("password")String password);
}
UserDao.xml
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from user where username = #{username}
insert into user (username,password)
values (#{username},#{password})
控制器
UserController.java
package com.sy.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 org.springframework.web.bind.annotation.SessionAttributes;
import com.sy.http://entity.User;
import com.sy.service.UserService;
@Controller
@RequestMapping("/user")
//这里用了@SessionAttributes,可以直接把model中的user(也就key)放入其中
//这样保证了session中存在user这个对象
@SessionAttributes("user")
public class UserController {
@Autowired
private UserService userServivce;
//正常访问login页面
@RequestMapping("/login")
public String login(){
return "login";
}
//表单提交过来的路径
@RequestMapping("/checkLogin")
public String checkLogin(User user,Model model){
//调用service方法
user = userServivce.checkLogin(user.getUsername(), user.getPassword());
//若有user则添加到model里并且跳转到成功页面
if(user != null){
model.addAttribute("user",user);
return "success";
}
return "fail";
}
//测试超链接跳转到另一个页面是否可以取到session值
@RequestMapping("/anotherpage")
public String hrefpage(){
return "anotherpage";
}
//注销方法
@RequestMapping("/outLogin")
public String outLogin(HttpSession session){
//通过session.invalidata()方法来注销当前的session
session.invalidate();
return "login";
}
@RequestMapping("/regist")
public String regist(){
return "regist";
}
@RequestMapping("/doRegist")
public String doRegist(User user,Model model){
System.out.println(user.getUsername());
userServivce.Regist(user);
return "success";
}
}
实体类
package com.sy.entity;
public class User {
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
相关服务
UserService.java (接口)
package com.sy.service;
import com.sy.entity.User;
//Service层接口
public interface UserService {
//检验用户登录
User checkLogin(String username,String password);
void Regist(User user);
}
UserServiceImpl.java
package com.sy.service.Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sy.dao.UserDao;
import com.sy.entity.User;
import com.sy.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
/*
* 检验用户登录业务
*
*/
public User checkLogin(String username, String password) {
User user = userDao.findByUsername(username);
if(user != null && user.getPassword().equals(password)){
return user;
}
return null;
}
@Override
public void Regist(User user) {
userDao.registerByUsernameAndPassword(user.getUsername(),user.getPassword());
}
}
其他配置文件
持久层相关配置文件 sprintg-dao.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
spring-service.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
logback.xml
mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
JSP视图文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
regist.jsp
<%--
Created by IntelliJ IDEA.
User: 86265
Date: 2018/1/30
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
welcome,${sessionScope.user.username}!
this is success page!
fail.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
u are loser,this is a fail page!
anotherpage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
${sessionScope.user.username}!!!!!
三、运行结果
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~