When I try to fetch the values from class, which were set in jsp, null is shown.
Following error is observed in dev mode:
ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'name' on 'class org.ravi.EmployeeAction: Error setting expression 'name' with value 't'
Below are my various pages
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="addEmployeeAction" class="org.ravi.EmployeeAction">
<interceptor-ref name="params" />
<interceptor-ref name="modelDriven"/>
<result name="success">/Add.jsp</result>
</action>
<action name="EmployeeAction" class="org.ravi.EmployeeAction" method="execute">
<interceptor-ref name="params" />
<interceptor-ref name="modelDriven"/>
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<s:form action="Add.jsp" name="addForm">
<table border="1" cellpadding="5">
<tr>
<th>Select</th>
<th>EmpID</th>
<th>Name</th>
<th>City</th>
<th>DoB</th>
</tr>
<tr>
<th><input type="radio" name="record"
onClick="radioValidate(this, 'record')" value="%{var}">
</th>
<th><s:property value="empid"/></th>
<th><s:property value="name"/></th>
<th><s:property value="city"/></th>
<th><s:property value="dob"/></th>
</s:form>
</body>
</html>
Add.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Add New User</h1>
<s:form action="EmployeeAction" >
<s:textfield label="Emp Id" name="empid" />
<s:textfield label="Name" name="name" />
<s:textfield label="City" name="city" />
<s:textfield label="DoB" name="dob" />
<s:submit />
</s:form>
</body>
</html>
EmployeeAction.java
package org.ravi;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class EmployeeAction extends ActionSupport implements ModelDriven<Employee> {
private static final long serialVersionUID = -8136507522861159378L;
private Employee employee=new Employee();
public Employee getEmployee()
{
return employee;
}
public void setEmployee(Employee employee)
{
this.employee=employee;
}
public String execute() throws Exception
{
return SUCCESS;
}
@Override
public Employee getModel()
{
return employee;
}
}
Employee.java
package org.ravi;
import java.io.Serializable;
public class Employee implements Serializable{
static final long serialVersionUID = 1L;
private String empid;
private String name;
private String city;
private String dob;
public void setempid(String empid) {
this.empid = empid;
}
public void setname(String name) {
this.name = name;
}
public void setcity(String city) {
this.city = city;
}
public void setdob(String dob) {
this.dob = dob;
}
public String getempid() {
return this.empid;
}
public String getname() {
return this.name;
}
public String getcity() {
return this.city;
}
public String getdob() {
return this.dob;
}
}
You're committing several errors, the worst is that
you're generating getters and setters manually (a lot of useless work), and you're also doing it wrongly: the first letter of the variable name must be capitalized:
setName(
instead ofsetname(
for variablename
.
You should (for simplicity and consistency) also do it for every word of your variables with more than one word:
setEmpId(
for variableempId
.
You should also consider avoiding the redundancy when possible. If you have an ID field on a class Employee
, just call it id
, not empId
... if it's inside Emp, it's obvious it's emp id and not something else id.
Use ModelDriven
only if you enjoy pain. For any other purpose, it is as useful as a sausage in your pocket when facing a pack of hungry stray dogs.
Use the HTML5 DTD <!DOCTYPE html>
also if you're targeting old browsers, there's no need to use 4.01
nowadays.
Never call JSPs directly like you do in your first form, always pass through actions first.
Start with this. There's plenty more.
Must read