一、作业1:
1.创建工程,导入相关jar包
2.创建pojo包
package com.openlab.pojo;
import java.util.Date;
public class User {
private Integer id;
private String userCode;
private String userName;
private String userPassword;
private Integer gender;
private Date birthday;
private String phone;
private String address;
private Integer userRole;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;
private String idPicPath;
private String workPicPath;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public String getIdPicPath() {
return idPicPath;
}
public void setIdPicPath(String idPicPath) {
this.idPicPath = idPicPath;
}
public String getWorkPicPath() {
return workPicPath;
}
public void setWorkPicPath(String workPicPath) {
this.workPicPath = workPicPath;
}
}
3.创建utils包
package com.openlab.utils;
import java.sql.*;
public class JDBCUtils {
static Connection connection = null;
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"
,"root","123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void closeResource(Connection connection, ResultSet resultSet, Statement statement) {
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4.创建dao包
package com.openlab.dao;
import com.openlab.pojo.User;
public interface UserDao {
User selectByUserCodeAndPassword(String userCode, String password);
}
5.创建dao.impl包
package com.openlab.dao.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.utils.JDBCUtils;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public User selectByUserCodeAndPassword(String userCode, String password) {
String sql = "select * from smbms_user where userCode = ? and userPassword = ?";
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,userCode);
preparedStatement.setString(2,password);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
user = new User();
user.setUserName(resultSet.getString("userName"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
6.创建service包
package com.openlab.service;
import com.openlab.pojo.User;
public interface UserService {
User login(String userCode, String password);
}
7.创建service.impl包
package com.openlab.service.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User login(String userCode, String password) {
return userDao.selectByUserCodeAndPassword(userCode,password);
}
}
8.创建配置文件
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.openlab.dao.impl"></context:component-scan>
<context:component-scan base-package="com.openlab.service.impl"></context:component-scan>
</beans>
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources location="/statics/" mapping="s/**"></mvc:resources>
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.openlab.controller"></context:component-scan>
</beans>
9.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
10.创建jsp
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/login">去往登录界面</a>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>用户登录界面</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/doLogin">
用户名:<input type="text" name="userCode"><br/>
密 码:<input type="password" name="password"><br/>
<span>${err}</span><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
mainPage.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>主页面</title>
</head>
<body>
你好,用户${user.userName},欢迎光临
<br/>
<img src="0721work2_war_exploded/../s/images/preview.jpg">
<a href="${pageContext.request.contextPath}/user/logout">退出登录</a>
</body>
</html>
11.创建controller包
package com.openlab.controller;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/login")
public String login(){
return "login";
}
@RequestMapping(value = "/doLogin",method = RequestMethod.POST )
public String doLogin(String userCode, String password, HttpServletRequest request, HttpSession session){
User user = userService.login(userCode, password);
if(user!=null){
session.setAttribute("user",user);
return "redirect:/user/check";
}else {
request.setAttribute("err","用户名或密码错误,登录失败");
return "login";
}
}
@RequestMapping("/check")
public String main(HttpSession session){
if(session.getAttribute("user")==null){
return "redirect:/user/login";
}else {
return "redirect:/mainPage.jsp";
}
}
@RequestMapping("/logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "redirect:/user/login";
}
}
12.测试
二、作业2
1.创建error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>错误页面</title>
</head>
<body>
服务器发生异常,请联系管理员
</body>
</html>
2.修改springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources location="/statics/" mapping="s/**"></mvc:resources>
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.openlab.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">
error
</prop>
</props>
</property>
</bean>
</beans>
3.修改UserController.java
package com.openlab.controller;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/login")
public String login(){
return "login";
}
@RequestMapping(value = "/doLogin",method = RequestMethod.POST )
public String doLogin(String userCode, String password, HttpServletRequest request, HttpSession session){
User user = userService.login(userCode, password);
if(user!=null){
session.setAttribute("user",user);
return "redirect:/user/check";
}else {
throw new RuntimeException("用户名或密码错误");
}
}
@RequestMapping("/check")
public String main(HttpSession session){
if(session.getAttribute("user")==null){
return "redirect:/user/login";
}else {
return "redirect:/mainPage.jsp";
}
}
@RequestMapping("/logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "redirect:/user/login";
}
}