Struts 使用 DynaValidatorForm 的项目给出 HTTP 状态 500 错误 - "Resources cannot be null"
Struts project with DynaValidatorForm gives HTTP status 500 error - "Resources cannot be null"
我正在学习 Struts 并且我已经使用 maven + hibernate + Struts 创建了一个项目。当我 运行 项目 JSP 加载但是当我提交数据时,出现以下错误 -
HTTP Status 500 - java.lang.IllegalArgumentException: Resources cannot be null.
我已经尽力了,但我找不到问题所在。下面是我的源代码。
Web.xml :-
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>Customer.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Struts-config.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="customerActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="name" type="java.lang.String"></form-property>
<form-property name="age" type="java.lang.String"></form-property>
<form-property name="contactNo" type="java.lang.String"></form-property>
<form-property name="email" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
<action-mappings>
<action path="/register" name="customerActionForm" parameter="parameter" type="com.openarc.struts.action.CustomerAction" input="Customer.jsp">
<forward name="success" path="index.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="errors"/>
errors.properties(之前用于验证器框架)
label.name = Name
label.age = Age
label.contactNo = Contact Number
label.email = Email
Customer.jsp :-
<body>
<html:form action="register">
<table>
<tr>
<td><bean:message key="label.name"/> </td>
<td><html:text property="name"></html:text></td>
</tr>
<tr>
<td><bean:message key="label.age"/> </td>
<td><html:text property="age"></html:text></td>
</tr>
<tr>
<td><bean:message key="label.contactNo"/> </td>
<td><html:text property="contactNo"></html:text></td>
</tr>
<tr>
<td><bean:message key="label.email"/> </td>
<td><html:text property="email"></html:text></td>
</tr>
<tr>
<td></td>
<td><html:submit property="parameter" value="Create Customer"></html:submit> </td>
</tr>
</table>
</html:form>
</body>
CustomerAction.java:-
public class CustomerAction extends DispatchAction{
public ActionForward createCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
DynaActionForm dynaForm = (DynaActionForm)form;
Customer customer = new Customer();
customer.setCustomerId(1);
customer.setName(dynaForm.getString("name"));
customer.setAge(Short.parseShort(dynaForm.getString("age")));
customer.setContactNo(dynaForm.getString("contactNo"));
customer.setEmail(dynaForm.getString("email"));
CustomerDAO customerDAO = new CustomerDAO();
customerDAO.createCustomer(customer);
return mapping.findForward("success");
}
}
CustomerDAO.java:-
public class CustomerDAO {
public Customer createCustomer(Customer customer){
Transaction transaction = null;
try{
Session session = HibernateUtil.openSession();
transaction = session.beginTransaction();
session.save(customer);
session.flush();
transaction.commit();
session.close();
}catch(Exception e){
transaction.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
return customer;
}
}
HibernateUtil.java :-
public class HibernateUtil {
private static final SessionFactory SESSIONFACTORY = createSessionFactory();
private static final ThreadLocal<Session> THREADLOCAL = new ThreadLocal<Session>();
private static SessionFactory createSessionFactory() {
try{
return new Configuration().configure().buildSessionFactory();
}catch(HibernateException e){
System.err.println("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
}
public static Session openSession(){
Session session = THREADLOCAL.get();
if(session == null){
session = SESSIONFACTORY.openSession();
THREADLOCAL.set(session);
}
return session;
}
public static void closeSession(){
Session session = THREADLOCAL.get();
if(session.isOpen()){
session.close();
THREADLOCAL.remove();
}
}
}
我在网上搜索时似乎是一个著名的错误,但我找不到我的代码到底有什么问题。任何帮助将不胜感激。
在struts-config.xml
的内容下方添加插件。希望它能解决您的问题。
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
并且不要忘记在 /WEB-INF/validation.xml
中初始化 validation.xml
文件
我正在学习 Struts 并且我已经使用 maven + hibernate + Struts 创建了一个项目。当我 运行 项目 JSP 加载但是当我提交数据时,出现以下错误 -
HTTP Status 500 - java.lang.IllegalArgumentException: Resources cannot be null.
我已经尽力了,但我找不到问题所在。下面是我的源代码。
Web.xml :-
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>Customer.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Struts-config.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="customerActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="name" type="java.lang.String"></form-property>
<form-property name="age" type="java.lang.String"></form-property>
<form-property name="contactNo" type="java.lang.String"></form-property>
<form-property name="email" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
<action-mappings>
<action path="/register" name="customerActionForm" parameter="parameter" type="com.openarc.struts.action.CustomerAction" input="Customer.jsp">
<forward name="success" path="index.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="errors"/>
errors.properties(之前用于验证器框架)
label.name = Name
label.age = Age
label.contactNo = Contact Number
label.email = Email
Customer.jsp :-
<body>
<html:form action="register">
<table>
<tr>
<td><bean:message key="label.name"/> </td>
<td><html:text property="name"></html:text></td>
</tr>
<tr>
<td><bean:message key="label.age"/> </td>
<td><html:text property="age"></html:text></td>
</tr>
<tr>
<td><bean:message key="label.contactNo"/> </td>
<td><html:text property="contactNo"></html:text></td>
</tr>
<tr>
<td><bean:message key="label.email"/> </td>
<td><html:text property="email"></html:text></td>
</tr>
<tr>
<td></td>
<td><html:submit property="parameter" value="Create Customer"></html:submit> </td>
</tr>
</table>
</html:form>
</body>
CustomerAction.java:-
public class CustomerAction extends DispatchAction{
public ActionForward createCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
DynaActionForm dynaForm = (DynaActionForm)form;
Customer customer = new Customer();
customer.setCustomerId(1);
customer.setName(dynaForm.getString("name"));
customer.setAge(Short.parseShort(dynaForm.getString("age")));
customer.setContactNo(dynaForm.getString("contactNo"));
customer.setEmail(dynaForm.getString("email"));
CustomerDAO customerDAO = new CustomerDAO();
customerDAO.createCustomer(customer);
return mapping.findForward("success");
}
}
CustomerDAO.java:-
public class CustomerDAO {
public Customer createCustomer(Customer customer){
Transaction transaction = null;
try{
Session session = HibernateUtil.openSession();
transaction = session.beginTransaction();
session.save(customer);
session.flush();
transaction.commit();
session.close();
}catch(Exception e){
transaction.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
return customer;
}
}
HibernateUtil.java :-
public class HibernateUtil {
private static final SessionFactory SESSIONFACTORY = createSessionFactory();
private static final ThreadLocal<Session> THREADLOCAL = new ThreadLocal<Session>();
private static SessionFactory createSessionFactory() {
try{
return new Configuration().configure().buildSessionFactory();
}catch(HibernateException e){
System.err.println("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
}
public static Session openSession(){
Session session = THREADLOCAL.get();
if(session == null){
session = SESSIONFACTORY.openSession();
THREADLOCAL.set(session);
}
return session;
}
public static void closeSession(){
Session session = THREADLOCAL.get();
if(session.isOpen()){
session.close();
THREADLOCAL.remove();
}
}
}
我在网上搜索时似乎是一个著名的错误,但我找不到我的代码到底有什么问题。任何帮助将不胜感激。
在struts-config.xml
的内容下方添加插件。希望它能解决您的问题。
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
并且不要忘记在 /WEB-INF/validation.xml
validation.xml
文件