我正在查看有关 Spring 自定义验证器的其他问题,但不幸的是我无法用建议的答案解决我的问题。
我的问题如下:我有一个实体 (Account),我创建了一个自定义验证器 (AccountValidator),我在控制器 (RegisterController) 中使用它,但它从未被调用,使用默认验证器。
我是不是忘记了什么?我附上了部分代码以帮助更好地理解我的问题。
验证器:
public class AccountValidator implements Validator{
@Override
public boolean supports(Class<?> clazz) {
return (Account.class).isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
//Validation code
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
@Autowired
private AccountValidator accountValidator;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setDisallowedFields("id");
binder.setValidator(accountValidator);
}
@RequestMapping(method = RequestMethod.GET)
@ModelAttribute
public Account register(Locale currentLocale){
Account account = new Account();
return account;
}
@RequestMapping(method = RequestMethod.POST)
public String handleRegister(@Valid …Run Code Online (Sandbox Code Playgroud) 我有一个Liferay门户网站和2个不同的网站或公司.他们每个人都有自己的主题和内容,但两个网站的徽标是相同的.
我知道我可以删除公共徽标并在每个主题中添加不同的徽标,但我想知道的是,是否可以从控制面板更改唯一站点的徽标.
我试图在我的应用程序中配置拦截器,但我无法使其工作.
在我的应用程序配置类中,我已按以下方式配置:
@Configuration
@EnableWebMvc
public class AppContextConfiguration extends WebMvcConfigurerAdapter {
...
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor());
}
...
}
Run Code Online (Sandbox Code Playgroud)
和拦截器:
public class MyInterceptor extends HandlerInterceptorAdapter{
private static final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
logger.debug("MyInterceptor - PREHANDLE");
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么没有被调用?
我开始用Spring MVC和Hibernate开发一个测试应用程序,我对数据库配置有疑问.
我知道我可以通过application-context.xml定义数据源,比如
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是我想根本不使用XML,所以我创建了一个配置类,我想在其中加载一个DataSource对象,方法类似于:
@Bean
public DataSource dataSource() {
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何获得指向我的MySQL模式的DataSource实例?如果有几种选择,您认为哪种选择最好?
我想使用MySQL数据库,而不是嵌入式数据库
谢谢
我无法使用@Repository注释类使@Autowire注释工作.
我有一个界面:
public interface AccountRepository {
public Account findByUsername(String username);
public Account findById(long id);
public Account save(Account account);
}
Run Code Online (Sandbox Code Playgroud)
实现用@Repository注释的接口的类:
@Repository
public class AccountRepositoryImpl implements AccountRepository {
public Account findByUsername(String username){
//Implementing code
}
public Account findById(long id){
//Implementing code
}
public Account save(Account account){
//Implementing code
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我需要使用此存储库通过用户名查找帐户,因此我使用自动装配,但我正在检查它是否有效并且accountRepository实例始终为null:
@Component
public class FooClass {
@Autowired
private AccountRepository accountRepository;
...
public barMethod(){
logger.debug(accountRepository == null ? "accountRepository is NULL" : "accountRepository IS NOT NULL");
}
} …Run Code Online (Sandbox Code Playgroud) spring ×3
spring-mvc ×3
annotations ×2
java ×2
autowired ×1
datasource ×1
hibernate ×1
interceptor ×1
liferay ×1
mysql ×1
repository ×1
themes ×1
validation ×1
web ×1