我建立的Spring MVC应用程序。 我想dissalow添加重复(用户名是主键)条目和视图通知选择另一个用户名。
我工作的注册用户选项。 一切正常,直到我把用户与已经存在的用户名。 好了,这是故意的,因为用户名列是我的数据库中的主键。
我要寻找一个选项,所以处理这个问题:
这是我的SQL表
create table users(
username varchar(50) not null primary key,
password varchar(50) not null);
这是我的仓库:
@Repository
class UserRepositoryImpl implements UserRepository{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addUser(User user) throws MySQLIntegrityConstraintViolationException {
/*language=SQL*/
String SQL_ADD_USER= "INSERT INTO users VALUES (?,?)";
String username = user.getUsername();
String password = user.getPassword();
jdbcTemplate.update(SQL_ADD_USER, username, password);
}
}
这是我的控制器类的一部分:
@RequestMapping(value="/register", method = RequestMethod.GET)
public String registerPage(@ModelAttribute("user") User user){return "register";}
@RequestMapping(value="/register", method=RequestMethod.POST)
public String processRegisterUser(@ModelAttribute("user") User user, BindingResult result){
try {
userRepository.addUser(user);
} catch (com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e) {
//some line of code that would add information to the view that user with given username already exists.
}
return "redirect:/login";
}
正如你可以看到我试着处理,添加重复的条目时发生异常。 这就是为什么我把这一条款throws MySQLIntegrityConstraintViolationException
在公共无效ADDUSER(用户用户)。 在接下来的步骤,我想处理此异常public String processRegisterUser
方法,这样我可以在认为给定用户名已被占用通知用户。 好了,这是行不通的。 我不能处理com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
和视图添加重复的错误,而不是警告时,我得到:
HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO users VALUES (?,?)]; Duplicate entry 'user' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user' for key 'PRIMARY'
type Exception report
message Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO users VALUES (?,?)]; Duplicate entry 'user' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user' for key 'PRIMARY'
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO users VALUES (?,?)]; Duplicate entry 'user' for key 'PRIMARY'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user' for key 'PRIMARY'
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
如果您需要任何详细信息,请发表评论。