- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于这个问题的许多回复让我感到压力很大,很多人说我需要使用注释,但我不认为这是这里的问题,也许我的配置有错误。它可能在我的 bean 接线中。我在两点上遇到错误1。当我跳出用户名字段时 - 因为我当时正在执行 Web 服务 和2。当我点击提交按钮并发布数据时。
我正在尝试检查数据库以查看用户名是否已存在,因此我使用了网络服务,因此当我跳出该字段时,它会检查数据库。当我将数据发布到服务器时,我也想进行相同的验证,因此我再次调用相同的函数。
该函数返回一个 boolean 值。它是类中的一个函数,接受 String userName 参数。也许我连接的 bean 不正确。如果我创建一个 Factory Bean 并创建一个 applicationContext.xml 的新实例,它可以工作:
FactoryBean.java
package com.crimetrack.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class FactoryBean {
private static ClassPathXmlApplicationContext context;
private static ClassPathXmlApplicationContext getContext() {
if (context == null) {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
return context;
}
public static OfficerRegistrationValidation getOfficerRegistrationValidation() {
return (OfficerRegistrationValidation) getContext().getBean("officerRegistrationValidation");
}
}
然后如果我使用
(FactoryBean.getOfficerRegistrationValidation().validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true){
在OfficerRegistrationValidation.java中它可以工作,但我不能在我的应用程序中使用它,因为它在applicationContext.xml中创建每个bean的新实例,这会影响我的应用程序。
下面是我的代码和两个错误日志:1.当我跳出用户名字段时;2.当我通过提交按钮发布表单时。我希望这能够很好地说明我正在努力实现的目标:
office_registration.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<style>
<%@ include file="../css/forms.css" %>
</style>
<script type="text/javascript">
<%@ include file="../js/off_reg.js"%>
$(document).ready(function(){
$('#userName').blur(function(evt){
CheckAvailability();
});
});
function CheckAvailability(){
$.ajax( {
type:'GET',
url:'validateUserName.htm',
data:{userName:$('#userName').val()},
dataType: 'json',
success:function(data) {
if (data == true){
alert("User Name Already Exists");
$("#userNameErr").text("User Name Already Exist");
}else if ($("#userName").val() == ""){
$("#userNameErr").text(" ");
}else if(data == false){
$("#userNameErr").text("User Name Valid");
}
}
});
}
</script>
<title>Officer Registration</title>
</head>
<body>
<form:form method="post" modelAttribute="officers" action="officer_registration.htm">
<ol>
<li><label>User Name</label>
<form:input path="userName"/><form:errors path="userName" id="errors"/><label id="userNameErr"></label></li>
<li><label>Password</label>
<form:password path="password"/><form:errors path="password" id="errors"/></li>
<li><label>Re-Enter Password</label>
<form:password path="password2"/><form:errors path="password2" id="errors"/></li>
<li><label>e-Mail Address</label>
<form:input path="emailAdd"/><form:errors path="emailAdd" id="errors"/></li>
<br/>
<li><input type="submit" name= "request" value="Register" />
<input type="submit" name= "request" value="Update" /></li>
</ol>
</form:form>
</body>
</html>
OfficerRegistrationValidation.java
public class OfficerRegistrationValidation implements Validator{
private final Logger logger = Logger.getLogger(getClass());
ValidateUserNameManager validateUserNameManager;
public boolean supports(Class<?> clazz) {
return Officers.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
Officers officer = (Officers) target;
if (officer.getUserName() == null){
errors.rejectValue("userName", "userName.required");
}else{
String userName = officer.getUserName();
logger.info("OfficerRegistrationValidation - UserName is not null so going to check if its valid for :" + userName);
try {
logger.info("OfficerRegistrationValidation - Just before try.....catch block...userName is :" + userName);
logger.info("OfficerRegistrationValidation - about to evaluate if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true)" );
//using a factory bean to instantiate the creation of the bean
//in some cases you want to use the existing bean and not instantiate
//if (FactoryBean.getOfficerRegistrationValidation().validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true){
if (validateUserNameManager.DoesUserNameExist(officer.getUserName())== true){
errors.rejectValue("userName", "userName.exist");
}
} catch (Exception e) {
logger.info("OfficerRegistrationValidation - Error Occured When validating UserName");
logger.error("Message", e);
errors.rejectValue("userName", "userName.error");
}
}
if(officer.getPassword()== null){
errors.rejectValue("password", "password.required");
}
if(officer.getPassword2()== null){
errors.rejectValue("password2", "password2.required");
}
}
/**
* @return the validateUserNameManager
*/
public ValidateUserNameManager getValidateUserNameManager() {
logger.info("Getting - ValidateUserNameManager");
return validateUserNameManager;
}
/**
* @param validateUserNameManager the validateUserNameManager to set
*/
public void setValidateUserNameManager(
ValidateUserNameManager validateUserNameManager) {
logger.info("Setting - ValidateUserNameManager");
this.validateUserNameManager = validateUserNameManager;
}
}
ValidateUserNameManager.java
public class ValidateUserNameManager implements ValidateUserNameIFace {
public ValidateUserNameManager(){}
private OfficersDAO officerDao;
private final Logger logger = Logger.getLogger(getClass());
public boolean DoesUserNameExist(String userName) throws Exception {
logger.info("Inside ValidateUserNameManager");
try{
logger.info("ValidateUserNameManager - UserName is : " + userName);
if(officerDao.OfficerExist(userName) == true){
logger.info("ValidateUserNameManager - UserName :" + userName + " does exist");
return true;
}else{
logger.info("ValidateUserNameManager - UserName :" + userName + " does NOT exist");
return false;
}
}catch(Exception e){
logger.error("Message", e);
logger.info("ValidateUserNameManager - UserName :" + userName + " EXCEPTION OCCURED " + e.toString());
return false;
}
}
/**
* @return the officerDao
*/
public OfficersDAO getOfficerDao() {
logger.info("ValidateUserNameManager - getting officerDAO");
return officerDao;
}
/**
* @param officerdao the officerDao to set
*/
public void setOfficerDao(OfficersDAO officerDao) {
logger.info("ValidateUserNameManager - setting officerDAO");
this.officerDao = officerDao;
}
}
OfficerRegistrationController.java
@Controller
public class OfficerRegistrationController {
private final Logger logger = Logger.getLogger(getClass());
private DivisionManager divisionManager;
private PositionManager positionManager;
private GenderManager genderManager;
private Officers officer = new Officers();
private ValidateUserNameManager validateUserNameManager;
Map<String, Object> myDivision = new HashMap<String, Object>();
Map<String, Object> myPosition = new HashMap<String, Object>();
Map<String, Object> myGender = new HashMap<String, Object>();
@InitBinder("officers")
protected void initBinder(WebDataBinder binder){
//removes white spaces
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
//formats date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//By passing true this will convert empty strings to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
dateFormat.setLenient(false);
logger.info("Just before initBinder");
binder.setValidator(new OfficerRegistrationValidation());
}
@RequestMapping(value="officer_registration.htm", method = RequestMethod.GET)
public ModelAndView loadPage(HttpServletRequest request,
HttpServletResponse response,@ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model) throws Exception {
try{
logger.debug("In Http method for OfficerRegistrationController");
myDivision.put("divisionList", this.divisionManager.getDivisions());
myPosition.put("positionList", this.positionManager.getPositionList());
myGender.put("genderList", this.genderManager.getGenderList());
model.addAttribute("division", myDivision);
model.addAttribute("position", myPosition);
model.addAttribute("gender", myGender);
return new ModelAndView("officer_registration");
}catch(Exception e){
request.setAttribute("error",e.getMessage());
return new ModelAndView("error_page");
}
}
@RequestMapping(value="officer_registration.htm", method=RequestMethod.POST)
public ModelAndView handleRequest(@Valid @ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model)throws Exception{
if(result.hasErrors()){
model.addAttribute("division", myDivision);
model.addAttribute("position", myPosition);
model.addAttribute("gender", myGender);
return new ModelAndView("officer_registration");
}else{
return null;
}
}
@RequestMapping(value="validateUserName.htm", method=RequestMethod.GET)
public @ResponseBody String validateUserName(@RequestParam String userName)throws Exception{
String results = "false";
logger.info("Inside OfficerRegistrationController");
try{
logger.info("In try ..... catch for OfficerRegistrationController");
if (validateUserNameManager.DoesUserNameExist(userName)== true){
results = "true";
return results;
}
}catch(Exception e){
logger.debug("Error in validateUserName Controller " + e.toString());
return results;
}
return results;
}
public void setDivisionManager(DivisionManager divisionManager){
this.divisionManager = divisionManager;
}
public void setPositionManager(PositionManager positionManager){
this.positionManager = positionManager;
}
public void setGenderManager(GenderManager genderManager){
this.genderManager = genderManager;
}
/**
* @return the validateUserNameManager
*/
public ValidateUserNameManager getValidateUserNameManager() {
return validateUserNameManager;
}
/**
* @param validateUserNameManager the validateUserNameManager to set
*/
public void setValidateUserNameManager(
ValidateUserNameManager validateUserNameManager) {
this.validateUserNameManager = validateUserNameManager;
}
/**
* @return the officer
*/
public Officers getOfficer() {
return officer;
}
/**
* @param officer the officer to set
*/
public void setOfficer(Officers officer) {
this.officer = officer;
}
}
crimetrack-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"..............
<!-- __________________________________________________________________________________________________ -->
<!-- Supports annotations and allows the use of @Controller, @Required, @RequestMapping -->
<context:annotation-config/>
<context:component-scan base-package="com.crimetrack" />
<!-- __________________________________________________________________________________________________ -->
<!-- Forwards requests to the "/" resource to the "login" view -->
<mvc:view-controller path="/login" view-name="login"/>
<!-- Forwards requests to the "/" resource to the "officer_registration" view -->
<mvc:view-controller path="/officer_registration" view-name="officer_registration"/>
<!-- __________________________________________________________________________________________________ -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> -->
<!-- Is used to process method level annotations -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- __________________________________________________________________________________________________ -->
<!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean name="/hello.htm" class="com.crimetrack.web.CountryListController">
<property name="countryManager" ref="countryManager"/>
</bean>
<bean name="/login.htm" class="com.crimetrack.web.AuthenticationController">
<property name="authenticationManager" ref="authenticationManager"/>
</bean>
<bean name="/officer_registration.htm" class="com.crimetrack.web.OfficerRegistrationController">
<property name="divisionManager" ref="divisionManager" />
<property name="positionManager" ref="positionManager" />
<property name="genderManager" ref="genderManager"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean name="/validateUserName.htm" class="com.crimetrack.web.OfficerRegistrationController">
<property name="validateUserNameManager" ref="validateUserNameManager"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"...............
<!-- __________________________________________________________________________________________________ -->
<bean id="countryManager" class="com.crimetrack.service.CountryManager">
<property name="countryDao" ref="countryDao"/>
</bean>
<bean id="countryDao" class="com.crimetrack.jdbc.JdbcCountryDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="authenticationManager" class="com.crimetrack.service.AuthenticationManager">
<property name="loginDao" ref="loginDao" />
</bean>
<bean id="loginDao" class="com.crimetrack.jdbc.JdbcLoginDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="divisionManager" class="com.crimetrack.service.DivisionManager">
<property name="divisionDao" ref="divisionDao"/>
</bean>
<bean id="divisionDao" class="com.crimetrack.jdbc.JdbcDivisionDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="positionManager" class="com.crimetrack.service.PositionManager">
<property name="positionDao" ref="positionDao"/>
</bean>
<bean id="positionDao" class="com.crimetrack.jdbc.JdbcPositionDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="genderManager" class="com.crimetrack.service.GenderManager">
<property name="genderDao" ref="genderDao"/>
</bean>
<bean id="genderDao" class="com.crimetrack.jdbc.JdbcGenderDAO" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="officerRegistrationValidation" class="com.crimetrack.service.OfficerRegistrationValidation">
<property name="validateUserNameManager" ref="validateUserNameManager"/>
</bean>
<bean id="validateUserNameManager" class="com.crimetrack.service.ValidateUserNameManager">
<property name="officerDao" ref="officerDao"/>
</bean>
<bean id="officerDao" class="com.crimetrack.jdbc.JdbcOfficersDAO" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
<强>1。 Tab 超出用户名字段时的错误日志
1882370 [http-8084-1] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public java.lang.String com.crimetrack.web.OfficerRegistrationController.validateUserName(java.lang.String) throws java.lang.Exception
1882370 [http-8084-1] INFO com.crimetrack.web.OfficerRegistrationController - Inside OfficerRegistrationController
1882370 [http-8084-1] INFO com.crimetrack.web.OfficerRegistrationController - In try ..... catch for OfficerRegistrationController
1882370 [http-8084-1] INFO com.crimetrack.service.ValidateUserNameManager - Inside ValidateUserNameManager
1882370 [http-8084-1] INFO com.crimetrack.service.ValidateUserNameManager - ValidateUserNameManager - UserName is : admin
1882371 [http-8084-1] ERROR com.crimetrack.service.ValidateUserNameManager - Message
java.lang.NullPointerException
at com.crimetrack.service.ValidateUserNameManager.DoesUserNameExist(ValidateUserNameManager.java:26)
at com.crimetrack.web.OfficerRegistrationController.validateUserName(OfficerRegistrationController.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
1882371 [http-8084-1] INFO com.crimetrack.service.ValidateUserNameManager - ValidateUserNameManager - UserName :admin EXCEPTION OCCURED java.lang.NullPointerException
2.表单POST到 Controller 时的错误日志:
1135560 [http-8084-1] DEBUG org.springframework.beans.TypeConverterDelegate - Converting String to [class java.lang.String] using property editor [org.springframework.beans.propertyeditors.StringTrimmerEditor@1983ad7]
1135560 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - UserName is not null so going to check if its valid for :admin
1135560 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - Just before try.....catch block...userName is :admin
1135560 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - about to evaluate if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true)
1135560 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - Error Occured When validating UserName
1135561 [http-8084-1] ERROR com.crimetrack.service.OfficerRegistrationValidation - Message
java.lang.NullPointerException
at com.crimetrack.service.OfficerRegistrationValidation.validate(OfficerRegistrationValidation.java:61)
at org.springframework.validation.DataBinder.validate(DataBinder.java:725)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:815)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:367)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
最佳答案
当您执行 new OfficerRegistrationValidation()
时在你的 Controller 中initBinder
你得到一个实例 validateUserNameManager
为空,因此 NPE。Spring 不会自动为您“填充”类字段,您需要请求它。
此外,您的 Controller 似乎在字段(类级别)上存储每个用户的内容,但如果两个用户请求同一页面怎么办?
我会建议类似的事情:
@Controller
public class OfficerRegistrationController {
private final Logger logger = Logger.getLogger(getClass());
@Autowire // tells spring to populates that for us.
private DivisionManager divisionManager;
@Autowire
private PositionManager positionManager;
@Autowire
private GenderManager genderManager;
@Autowire
private OfficerRegistrationValidation officerRegistrationValidation;
@InitBinder("officers")
protected void initBinder(WebDataBinder binder){
//removes white spaces
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
//formats date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//By passing true this will convert empty strings to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
dateFormat.setLenient(false);
logger.info("Just before initBinder");
binder.setValidator(officerRegistrationValidation);
}
@RequestMapping(value="officer_registration.htm", method = RequestMethod.GET)
public ModelAndView loadPage(HttpServletRequest request, HttpServletResponse response,@ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model) throws Exception {
// ...
}
@RequestMapping(value="officer_registration.htm", method=RequestMethod.POST)
public ModelAndView handleRequest(@Valid @ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model) throws Exception{
// ...
}
@RequestMapping(value="validateUserName.htm", method=RequestMethod.GET)
public @ResponseBody String validateUserName(@RequestParam String userName) throws Exception{
// ...
}
}
@Component
public class OfficerRegistrationValidation implements Validator {
private final Logger logger = Logger.getLogger(getClass());
@Autowire
ValidateUserNameManager validateUserNameManager;
public boolean supports(Class<?> clazz) {
return Officers.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
// ...
}
}
@Service
public class ValidateUserNameManager implements ValidateUserNameIFace {
private final Logger logger = Logger.getLogger(getClass());
@Autowire
private OfficersDAO officerDao;
public boolean doesUserNameExist(String userName) throws Exception {
// ...
}
}
@Repository
public class OfficersDAO {
// ...
}
注意 @Component
, @Service
, @Repository
对于类,这告诉 spring 在上下文启动时为这些类创建一个 bean(请参阅 <context:annotation-config/>
和 <context:component-scan base-package="..." />
文档)。 @Autowire
告诉 spring 尝试将带注释的字段与一个现有 bean 进行匹配(使用它的类型)。
整个想法是三层: Controller 、服务和 daos;
我也建议一些好的阅读:) :
关于java - SpringMvc java.lang.NullPointerException - 配置是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12643944/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!