接上一篇博客
代码有改动和增加的我都贴在下面了
UserController 中代码
package com.yiju.controller;
import com.yiju.bean.UserInfo;
import com.yiju.service.IUserService;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("login.do")
@ResponseBody
public String login(@Param(value = "username") String username,
@Param(value = "password") String password){
if ("123".equals(username)&&"123".equals(password)){
return "success";
}
return "fail";
}
@RequestMapping("login1.do")
@ResponseBody
public String login1(@Param(value = "username") String username,
@Param(value = "password") String password){
JSONObject jsonObject=new JSONObject();
if ("123".equals(username)&&"123".equals(password)){
jsonObject.put("result","1");
}else {
jsonObject.put("result","2");
}
return jsonObject.toString();
}
@RequestMapping("login2.do")
@ResponseBody
public String login2(@Param(value = "phone") String phone,
@Param(value = "password") String password,
HttpSession session){
JSONObject jsonObject=new JSONObject();
UserInfo user=userService.findUserByPhone(phone);
if (user==null){
jsonObject.put("result","0");
}else if (!password.equals(user.getPassword())){
jsonObject.put("result","1");
}else {
session.setAttribute("user",user);
jsonObject.put("result","2");
}
return jsonObject.toString();
}
@RequestMapping("signup.do")
@ResponseBody
public String signup(@Param(value = "phone") String phone,
@Param(value = "password") String password){
JSONObject jsonObject=new JSONObject();
UserInfo user=userService.findUserByPhone(phone);
if (user==null){
userService.add(phone,password);
jsonObject.put("result","0");
}else {
jsonObject.put("result","1");
}
return jsonObject.toString();
}
}
IUserDao 中代码
package com.yiju.dao;
import com.yiju.bean.UserInfo;
import org.apache.ibatis.annotations.Param;
public interface IUserDao {
UserInfo findUserByPhone(String phone);
void signup(@Param("phone") String phone, @Param("password") String password);
}
IUserService 中代码
package com.yiju.service;
import com.yiju.bean.UserInfo;
public interface IUserService {
UserInfo findUserByPhone(String phone);
void add(String phone, String password);
}
UserService 中代码
package com.yiju.service.Impl;
import com.yiju.bean.UserInfo;
import com.yiju.dao.IUserDao;
import com.yiju.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public UserInfo findUserByPhone(String phone) {
return userDao.findUserByPhone(phone);
}
@Override
public void add(String phone, String password) {
userDao.signup(phone,password);
}
}
basehead.jsp中代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>页头</title>
<!-- 下面是几个导入的包-->
<link type="text/css" href="${pageContext.request.contextPath}/css/css.css" rel="stylesheet" />
<script type="text/javascript" src="${pageContext.request.contextPath}/js/js.js"></script>
</head>
<body>
<!--头部最上方的框-->
<div class="header">
<div class="width1190">
<div class="fl" style="font-size: 14px">您好,欢迎来到<a href="${pageContext.request.contextPath}/index.jsp">易居住房信息平台!</a></div>
<div class="fr">
<a href="${pageContext.request.contextPath}/pages/login.jsp" style="font-size: 14px" target="_blank"><strong>登录</strong></a> |
<a href="${pageContext.request.contextPath}/pages/signup.jsp" style="font-size: 14px" target="_blank"><strong>注册</strong></a>
<a style="font-size: 14px">欢迎使用</a> |
<a href="pages/personal.jsp" style="font-size: 14px" target="_blank"><strong>个人中心</strong></a> |
<a href="#" style="font-size: 14px"><strong>退出</strong></a> |
<a href="javascript:;" onclick="" style="font-size: 14px">加入收藏</a&