SrpingMVC

二话不说,直接进入主题!

[b][color=blue]代码[/color][size=x-small][/size][/b]
package cn.u6.contoller;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import cn.u6.contoller.bean.UserBean;
import cn.u6.exception.MVCException;
import cn.u6.model.User;
import cn.u6.service.UserService;
import cn.u6.validator.UserValidator;

@Controller
@SessionAttributes("user")
public class UserController {

protected static Logger logger = Logger
.getLogger("cn.u6.contoller.UserController");

@Resource(name = "userService")
private UserService userService;

/**
* 登录
*/

@RequestMapping(value = "/user/login.do", method = RequestMethod.POST)

public String login(@ModelAttribute("user")UserBean user, BindingResult result,
SessionStatus status, Model model) throws Exception {
logger.debug("Received request to edit existing person");

// 验证用户输入信息
new UserValidator().validate(user, result);
try {
if (result.hasErrors()) {
return "redirect:/";
} else {
int id = userService.login(user.getUserName(), user
.getUserName(), user.getPassword());
switch (id) {
case -1:
model.addAttribute("message", "用户名错误!");
return "login";
case -2:
model.addAttribute("message", "密码错误!");
return "login";
default:
status.setComplete();
model.addAttribute("user", user);
// Session
// session
return "main";
}
}
} catch (UncategorizedSQLException e) {
logger.info("数据库连接出错:" + e.getMessage());
throw new SQLException("数据库连接出错:" + e.getMessage());
} catch (SQLException e) {
logger.info("数据库查询出错:" + e.getMessage());
throw new SQLException("数据库查询出错:" + e.getMessage());
} catch (NumberFormatException e) {
logger.info("数据转换出错:" + e.getMessage());
throw new NumberFormatException("数据转换出错:" + e.getMessage());
} catch (Exception e) {
logger.info("系统出错:" + e.toString() + " ----" + e.getMessage());
throw new MVCException("系统出错:" + e.getMessage());
}
}
}

这种情况下会运行之后发现以下错误:
[color=red][b]Session attribute 'user' required - not found in session.[/b][/color]

解决方法:
/**
* 登录
*/

@RequestMapping(value = "/user/login.do", method = RequestMethod.POST)
[b][color=red]@ModelAttribute("user")[/color][/b]
public String login(UserBean user, BindingResult result,
SessionStatus status, Model model) throws Exception
将这句话放到方法上面之后问题解决,本人在这个问题上花了一个上午...各位看客有什么其他的建议或方法可以评论评论~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值