对新接触springmvc的朋友来说,看过这票文章,我想会有一个不错的提升,另外,我在这里用了log4j2来打印日志,想了解的log4j2的可参考:http://blog.csdn.net/zouxucong/article/details/56013053
项目jar下载位置:http://download.csdn.net/detail/zouxucong/9784188
这里我用了log4j2,如果想用log4j2的朋友,在引用jar包、配置无误后,却还是无法使用,可以换一下tomcat的版本,用7.0.68,下载地址:http://download.csdn.net/detail/zouxucong/9769178
工程的大致结构如图:
第一步:创建Dynamic Web Project项目,在lib文件下导入jar包,并Build Path
第二步:配置web.xml文件,里面的配置主要有三部分
1)配置log4j2的引用
2)配置前端控制器以及中文乱码解决
代码如下:web.xml
RequestMappingTest
log4jConfiguration
classpath:log4j2.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-config.xml
1
springmvc
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
第三步:log4j2.xml以及springmvc的配置文件springmvc-config.xml
这里为了练习,我的log4j2.xml直接放在src下:
${sys:catalina.base}/var/logs
${LOG_HOME}/requestMappingTest
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M --
%msg%xEx%n
springmvc-config.xml:
/WEB-INF/context/
.jsp
第四步:jsp页面,路径:/WEB-INF/context/
loginForm.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录页面
登录页面
registerForm.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
注册页面
注册页面
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登陆成功页面
欢迎${requestScope.user.username }登录
第四步:实体类以及controller类
User.java:
package org.fkit.domain;
public class User {
private String loginname;
private String password;
private String username;
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
UserController.java:
package org.fkit.controller;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.fkit.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value="user")
public class UserController {
//静态List集合,此处代替数据库用来保存注册的用户信息
private static List userList;
//UserController类的构造器,初始化List集合
public UserController(){
super();
userList = new ArrayList();
}
//静态日之类loggerMaggent
private static final Logger logger = LogManager.getLogger(UserController.class.getName());
@RequestMapping(value="/register",method=RequestMethod.GET)
public String registerForm(){
logger.info("register GET 方法被调用……");
return "registerForm";
}
@RequestMapping(value="/register",method=RequestMethod.POST)
public String register(@RequestParam("loginname")String loginname,
@RequestParam("password")String password,
@RequestParam("username")String username){
logger.info("register POST 方法被调用……");
//创建user对象
User user = new User();
user.setLoginname(loginname);
user.setPassword(password);
user.setUsername(username);
//模拟数据库存储User信息
userList.add(user);
return "loginForm";
}
//登录
@RequestMapping("/login")
public String login(@RequestParam("loginname")String loginname,
@RequestParam("password")String password,Model model){
logger.info("登录名:"+loginname+" 密码:"+password);
//到集合中查找用户是否存在,此处用来模拟数据库验证
for(User user:userList){
if(user.getLoginname().equals(loginname) && user.getPassword().equals(password)){
model.addAttribute("user", user);
return "welcome";
}
}
return "loginForm";
}
}
部署RequestMappingTest这个Web应用,在浏览器输入如下URL来测试应用:
http://localhost:8080/RequestMappingTest/user/register
可看到如图所示界面,这里我用的虚拟路径request:
到这里,用springmvc实现简单登陆的过程就算实现了,希望可以帮到您,感觉有帮助的,麻烦点个赞,谢谢!