版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenbetter1996/article/details/83512453
spring mvc配置文件
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 包扫描,将注解的类注入 -->
<context:component-scan base-package="xyz.cglzwz.*"/>
<!-- 设置默认配置方案 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
<!-- 前缀(目录) -->
<property name="prefix" value="/" />
<!-- 后缀(文件名) -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
配置视图解析器后控制器可以直接返回String类型的页面文件名来指定需要跳转的页面视图
控制器代码
package xyz.cglzwz.controller;
import org.apache.ibatis.logging.Log;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import xyz.cglzwz.domain.User;
import xyz.cglzwz.service.UserService;
/**
* 用户登录控制器
*
* @author chgl16
* @date 2018-10-28 23:31
* @version 1.0
*/
@Controller
public class LoginController {
private final Logger log = Logger.getLogger(this.getClass());
@Autowired
private UserService userService;
@Autowired
private User user;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(@RequestParam String username, @RequestParam String password) {
user.setUsername(username);
user.setPassword(password);
if (userService.isRegister(user)) {
// 已经注册
log.info("用户" + username + "已经注册,可以登录");
if (userService.isCorrectPassword(user)) {
// 密码正确
log.info("密码正确, 正在登录....");
// 会跳转到index.jsp (配置了url视图配置器)
return "index";
} else {
log.info("密码错误");
return "errorPassword";
}
} else {
log.info("用户" + username + "尚未注册");
return "errorNoRegistered";
}
}
}