我有一个带有验证的登录页面。但是问题是,即使我输入了 ReloadableResourceBundleMessageSource ,只要用户输入错误的数据,它都应该显示错误消息,而不是抛出错误在代码下找不到消息... 。在我的 RootAppConfig.class 中。
注意::写在application.properties中的属性消息,放置在 src / main / resources / application.properties 中,我的jsp文件放置在 webapp / web中-inf / pages / insert.jsp
RootAppConfig.clas
------------------
@SpringBootApplication
@Import(value= {PersistenceConfig.class,AopConfig.class,ServiceConfig.class})
public class RootAppConfig {
public static void main(String[] args) {
SpringApplication.run(RootAppConfig.class, args);
}
@Bean
public MessageSource createSouce() {
ReloadableResourceBundleMessageSource source= new ReloadableResourceBundleMessageSource();
source.setBasename("classpath:application");//I have tried only application too
return source;
}
}
ServletInitializer.class
--------------------------
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MvcAppConfig.class,RootAppConfig.class);
}
}
application.properties
-----------------------------
#in-built container
server.port= 8081
#datasource
spring.datasource.jndi-name= java:/comp/env/DsJndi
#view resolver
spring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp
#form vallidator
name.required= Employee Name Cannot Be Empty
mobile.required= Mobile Number Is Not Valid
address.required= Address Cannot Be Empty
company.required= Company Name Cannot Be Empty
salary.required= Salary Field Cannot Be Empty
EmployeeValidator.class
----------------------------
@Component
public class EmployeeVallidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
// TODO Auto-generated method stub
return clazz.isAssignableFrom(EmployeeCommand.class);
}
@Override
public void validate(Object command, Errors errors) {
EmployeeCommand cmd= null;
cmd= (EmployeeCommand) command;
if(cmd.getName().equals("")||cmd.getName().length()==0) {
errors.rejectValue("name", "name.required");
}
if(Long.toString(cmd.getMobile()).length()<10||Long.toString(cmd.getMobile()).equals("")) {
errors.rejectValue("mobile", "mobile.required");
}
if(cmd.getAddress().equals("")||cmd.getAddress().length()==0) {
errors.rejectValue("address", "address.required");
}
if(cmd.getCompany().equals("")||cmd.getCompany().length()==0) {
errors.rejectValue("company", "company.required");
}
if(cmd.getSalary()<=0) {
errors.rejectValue("salary", "salary.required");
}
}
控制器
-----------------
public class AppController {
@Autowired
private EmployeeVallidator vallidator;
@Autowired
private EmployeeService service;
@RequestMapping(value="/home")
public String showHome() {
return "homePage";
}
@RequestMapping(value="/success")
public String returnSuccess() {
return "success";
}
@RequestMapping(value="/insert",method= RequestMethod.GET)
public String showInsertPage(@ModelAttribute("empCmd")EmployeeCommand cmd) {
return "insert-record";
}
@RequestMapping(value="/insert",method=RequestMethod.POST)
public String processRegistration(@ModelAttribute("empCmd")@Valid EmployeeCommand cmd,BindingResult errors,HttpSession session) {
EmployeeDto dto= null;
String result= null;
if(vallidator.supports(EmployeeCommand.class)) {
vallidator.validate(cmd, errors);
if(errors.hasErrors()) {
return "insert-record";
}
}
dto= new EmployeeDto();
BeanUtils.copyProperties(cmd, dto);
try {
result= service.pushEmployee(dto);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.setAttribute("message", result);
return "redirect:/success";
}
insert.jsp
----------------
<body>
<h1 align="center">Registration Form</h1>
<form:form method="POST" modelAttribute="empCmd">
<table border="0" cellpadding="0" cellspacing="0" width="480px" align="center">
<tr>
<td align="center">Name:</td>
<td><form:input path="name"/></td>
<td style="color: red;"><form:errors path="name" cssClass="errors"></form:errors></td>
</tr>
<tr><td> </td></tr>
<tr>
<td align="center">Mobile:</td>
<td><form:input path="mobile"/></td>
<td style="color: red;"><form:errors path="mobile" cssStyle="error"></form:errors></td>
</tr>
<tr><td> </td></tr>
<tr>
<td align="center">Address:</td>
<td><form:input path="address"/></td>
<td style="color: red;"><form:errors path="address" cssClass="errors"></form:errors></td>
</tr>
<tr><td> </td></tr>
<tr>
<td align="center">Company:</td>
<td><form:input path="company"/></td>
<td style="color: red;"><form:errors path="company" cssClass="errors"></form:errors></td>
</tr>
<tr><td> </td></tr>
<tr>
<td align="center">Net Salary:</td>
<td><form:input path="salary"/></td>
<td style="color: red;"><form:errors path="salary" cssClass="errors"></form:errors></td>
</tr>
<tr><td> </td></tr>
<table border='0' cellpadding='0' cellspacing='0' width='480px' align='center'>
<tr>
<td align='center'><input type='submit' name='REGISTER' value="register"></td>
</tr>
</table>
</table>
</form:form>
</body>
StackTrace
---------------
Caused by: org.springframework.context.NoSuchMessageException: No message found under code 'name.required.empCmd.name' for locale 'en_US'.
at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:91) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1276) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.support.RequestContext.getMessage(RequestContext.java:725) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.support.BindStatus.initErrorMessages(BindStatus.java:308) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.support.BindStatus.getErrorMessages(BindStatus.java:278) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.tags.form.ErrorsTag.exposeAttributes(ErrorsTag.java:319) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.tags.form.AbstractHtmlElementBodyTag.writeTagContent(AbstractHtmlElementBodyTag.java:48) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:86) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:83) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.jsp.WEB_002dINF.pages.insert_002drecord_jsp._jspService(insert_002drecord_jsp.java:179) ~[na:na]
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) ~[jasper.jar:8.0.50]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[servlet-api.jar:na]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) ~[jasper.jar:8.0.50]
... 66 common frames omitted
pom.xml
------------
<name>Save-Retrieve-Opearation-1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>