新建一个Web项目,在这里,命名为NewsPlatform。将smm中的包,复制到Web App Libraries中。
将之前的 applicationContext.xml、mybatis-config.xml、servlet-mvc、log4j.properties复制到src根目录下。注意,添加build path。
在这里的代码:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>display-name>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<filter>
<filter-name>EncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>EncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:servlet-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<error-page>
<error-code>500error-code>
<location>/500.jsplocation>
error-page>
<error-page>
<error-code>404error-code>
<location>/404.jsplocation>
error-page>
web-app>
在entity包中,建一个实体类,User,包括的属性有编号、姓名和密码。
package com.jereh.entity;
/**
* 用户类
* @author Administrator
*
*/
public class User {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
package com.jereh.dao;
import com.jereh.entity.User;
public interface UserDao {
/**
* 查询是否存在用户
* @return
*/
User findUser(User user);
}
package com.jereh.service;
import com.jereh.entity.User;
/**
* 用户服务,处理用户的业务
* @author Administrator
*/
public interface UserService {
User login(User user);
}
在index.jsp中,写一个登陆页面
写一个welcome.jsp的欢迎页面,登陆之后,跳转到welcome页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting pagetitle>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
head>
<body>
欢迎您,尊敬的${user.name}
body>
html>
<mapper namespace="com.jereh.dao.UserDao">
<select id="findUser" parameterType="User" resultType="User">
select * from user
<where>
<if test="id>0">
and id=#{id}
if>
<if test="name!=null">
and name=#{name}
if>
<if test="pwd!=null">
and pwd=#{pwd}
if>
where>
select>
mapper>
package com.jereh.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.jereh.dao.UserDao;
import com.jereh.entity.User;
import com.jereh.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public User login(User user) {
// TODO Auto-generated method stub
return userDao.findUser(user);
}
}
package com.jereh.controller;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jereh.entity.User;
import com.jereh.service.UserService;
@Controller
@RequestMapping("user/")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("login")
public String login(User user,Map map,HttpSession session) {
//登录
User resultUser=userService.login(user);
if(resultUser!=null) {
//跳转welcome界面
session.setAttribute("user", resultUser);
return "welcome";
} else {
//跳转到index界面
//登录时填写的用户数据
map.put("user", user);
//错误信息
map.put("error", "用户名或密码错误,请重新填写");
return "index";
}
}
}
package com.jereh.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
/**
* 统一异常处理
* @author Administrator
*
*/
public class HandlerException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
// TODO Auto-generated method stub
//定位到一个新页面
ModelAndView view=new ModelAndView();
view.setViewName("excep");
ex.printStackTrace();
return view;
}
}