xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee " versioncontextConfigLocationclasspath:spring-*.xmlorg.springframework.web.context.ContextLoaderListenercharacterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingutf-8characterEncodingFilter/*springmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring-mvc.xmlspringmvc/
9、编写服务端代码
编写User实体类,代码如下:
package cn.temptation.domain; /** * 用户信息 User { Integer userid; private String username; private String password; User() { super(); } public User(Integer userid, String username, String password) { super(); this.userid = userid; this.username = username; this.password = password; } Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } 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; } }
编写UserController控制器,代码如下:
package cn.temptation.web; import javax.annotation.Resource; import javax.servlet.http.HttpSession; 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 cn.temptation.dao.UserDao; import cn.temptation.domain.User; /** * 用户控制器 */ @Controller @RequestMapping(value = "/user") public class UserController { @Resource private UserDao userDao; @RequestMapping("/view") public String view() { return "main/login"; } @RequestMapping("/indexview") public String index() { return "main/index"; } @RequestMapping(value = "/login", method = RequestMethod.POST) public ModelAndView login(User model, HttpSession session) { User user = userDao.findByUsername(model.getUsername()); if (user == null || !user.getPassword().equals(model.getPassword())) { return new ModelAndView("redirect:/login.jsp"); } else { session.setAttribute("user", user); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } } }
编写UserDao数据访问层接口,代码如下:
package cn.temptation.dao; import cn.temptation.domain.User; public interface UserDao { public abstract User findByUsername(String username); }
编写UserMapper.xml数据访问层映射文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.temptation.dao.UserDao"> <select parameterType="string" resultType="User"> SELECT * FROM userinfo WHERE username=#{username} </select> </mapper>
10、编写客户端代码
编写login.jsp登录页,代码如下:
登录账号:密码:
编写index.jsp主页,代码如下:
主页欢迎,${user.username }
11、使用maven构建项目
在项目上右键,找到Maven属性的Update Project,也可以Alt+F5操作
修改项目编译路径为JRE系统类库,否则后续操作出错
项目上右键,找到Run As属性,找到Maven install
点击执行,结果如下说明构建成功
项目运行还是使用web工程的部署运行方式
运行项目,观察是否报错
登录页面
登录成功,跳转到主页