1.新建一个web项目,名称是spring mvc
2.因为resource包下面放的是配置文件,所以鼠标放在resource包上右键Build Path——Use As Resource Folder,也就是把spring的配置文件放到classpath下,不然到时候会找不到自定义的spring配置文件,最终效果如图
3.必备jar包
commons-logging:程序运行记录日志
spring-asm:spring独立的asm程序,是一种字节码技术,主要是用来反射的时候提升性能,如果单纯用jdk的反射,性能低,使用字节码增强技术后反射调用的时间基本和直接调用相当了
spring-beans:
spring-context:
spring-core:
spring-expression:
spring-web:
spring-webmvc:
4.配置web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>springDemo</display-name>
<!-- 设置编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- encoding用来指定一个具体的字符集 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- forceEncoding影响response中的字符编码 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<!-- spring控制转发配置-->
<servlet>
<servlet-name>springDemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置spring.xml文件的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDemo</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- 欢迎页设置 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
要引入context相关的命名空间
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- context:component-scan告诉spring要扫描的包 -->
<context:component-scan base-package="com.spring.*"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WebContent"
p:suffix=".jsp"/>
</beans>
5.每个类的代码
user.java,用户类,简单定义了登录账号,密码和真是姓名三个属性
package com.spring.bean;
public class User {
private String username;
private String password;
private String realname;
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;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
}
UserDao.java:接口,定义了登录,和注册两个方法
package com.spring.dao;
import org.springframework.stereotype.Service;
import com.spring.bean.User;
/**
* 用户操作接口
*
* @author wb-huaernan
*
*/
// @Service:用于标记接口
@Service("userDao")
public interface UserDao {
/**
* 根据账号密码登录
*
* @param username
* @param password
* @return
*/
public User userLogin(String username, String password);
/**
* 用户注册
*
* @param userBean
* @return
*/
public boolean userRegister(User user);
}
UserDaoImpl.java:接口实现类
package com.spring.daoImpl;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import com.spring.bean.User;
import com.spring.dao.UserDao;
/**
* 用户接口实现类
*
* @author wb-huaernan
*
*/
// @Scope表示用单例模式
@Scope("singleton")
// @Repository表示此类是个实现类,也就是dao层的实现类
@Repository(value = "userDao")
public class UserDaoImpl implements UserDao {
/**
* 用户登录
*/
@Override
public User userLogin(String username, String password) {
User user = null;
// 一般这里是从数据库查询,来获取userBean的相关信息的,现在简单写
if (username.equals("admin") && password.equals("admin")) {
user = new User();
user.setRealname("管理员");
}
return user;
}
/**
* 用户注册
*/
@Override
public boolean userRegister(User user) {
boolean result = false;
// 一般这里是执行数据库插入,这里简单判断下有值,就返回true,说明注册成功
if (!user.getUsername().isEmpty() && !user.getPassword().isEmpty()
&& !user.getRealname().isEmpty()) {
result = true;
}
return result;
}
}
UserAction.java:业务处理类
package com.spring.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.spring.bean.User;
import com.spring.dao.UserDao;
//@Controller:负责注册一个bean 到spring上下文中,用于标注控制层组件
//@RequestMapping:注解为控制器指定可以处理哪些 URL 请求
@Controller
@RequestMapping("/user")
public class UserAction {
/*
* @Autowired和@Resource都可以用来装配bean,都写在字段或者方法上
*
* @Autowired:默认按类型装配,依赖对象必须存在,如果要允许null值,
* 可以设置它的required属性为false,如:@Autowired(required=false)
* 如果我们想使用名称装配可以结合@Qualifier注解进行使用,如
*
* @Autowired() @Qualifier("userDao")
*
* @Resource默认按照名称进行装配,按照name指定,如果没写,默认按照属性名称匹配,推荐使用
*/
@Resource(name = "userDao")
private UserDao userDao;
/**
* 用户登录
*
* @param username
* @param password
* @return
*/
@RequestMapping(value = "login", method = RequestMethod.POST)
// 页面表单属性名称会自动和参数名称匹配起来
public ModelAndView UserLogin(String username, String password) {
User user = userDao.userLogin(username, password);
ModelAndView mav = new ModelAndView();
// forward:控制转发--redirect:重定向
mav.setViewName("forward:/welcome.jsp");
mav.addObject("user", user);
return mav;
}
/**
* 用户注册
*
* @param user
* @return
*/
@RequestMapping("register")
// 表单属性名称会自动和user的属性名称匹配起来
public ModelAndView UserRegister(User user) {
ModelAndView mav = new ModelAndView();
boolean result = userDao.userRegister(user);
mav.setViewName("redirect:/welcome.jsp");
mav.addObject("result", result);
return mav;
}
}
6.jsp页面,login.jsp,用户登录。userRegister.jsp用户注册,welcome.jsp注册或者登录成功后展示的页面
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="user/login.htm" method="post">
登录账号:<input type="text" name="username"><br/>
登录密码:<input type="password" name="password"><br/>
<input type="submit" value="登录">
<input type="button" οnclick="enterRegisterPage()" value="注册">
</form>
<!-- js打开注册页面 -->
<script type="text/javascript">
function enterRegisterPage(){
window.open("userRegister.jsp");
}
</script>
</body>
</html>
userRegister.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>
</head>
<body>
<form action="user/register.htm" method="post">
登录账号:<input type="text" name="username"><br/>
登录密码:<input type="text" name="password"><br/>
真实姓名:<input type="text" name="realname"><br/>
<input type="submit" value="确定">
<input type="reset" value="重置">
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.spring.bean.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎-spring-mvc项目欢迎页</title>
</head>
<body>
<%
//用户登录
User user =(User)request.getAttribute("user");
if(user!=null){
out.print("<h1>欢迎您,"+user.getRealname()+"</h1>");
}
//用户注册
String result = request.getParameter("result");
if(result.equals("true")){
out.print("<h1>注册成功!</h1>");
}
%>
</body>
</html>