1

I am writing a struts+spring+jooq integration sample webapp.

I have a employee table in db and a jsp where in I can search for an employee in the db and show details. Problem is on clicking search nothing happens. There is no error in logs as well my print statements in searchEmp action don't show anything on console. Can you please tell me why the struts action method is not being invoked?Here are the details

index.jsp file

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2 Spring integration </title>
</head>
<body>
<s:form>
       <s:textfield label="Enter Id" name="eid" value="Enter employee name   here.."></s:textfield>
    <s:textfield label="Enter name" name="name" value="Enter name here..">      </s:textfield>
<s:textfield label="Enter city" name="city" value="Enter city here..">    </s:textfield>

    <s:submit value="Create new employee" action="saveEmpAction"/>
    <s:submit value="Update Employee" action="updateEmpAction"></s:submit>
    <s:submit value="Delete Employee" action="deleteEmpAction"></s:submit>
    <s:submit value="Search Employee" action="searchEmpAction"></s:submit>  
</s:form>

<s:property value="message"/>
<hr>
<table border="1">
    <s:iterator value="emps">
        <tr>
            <td><s:property value="eid"/></td>
            <td><s:property value="name"/></td>
            <td><s:property value="city"/></td>
        </tr>
    </s:iterator>
</table>

</body>
</html>

Here is my EmpAction.java class

package com.rewardsapp.action;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import test.generated.tables.pojos.Emp;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.rewardsapp.dao.EmpDao;

public class EmpAction extends ActionSupport implements ModelDriven<Emp>,   Preparable{


/**
 * 
 */
private static final long serialVersionUID = -2435427486571261741L;

Emp emp;
EmpDao empdao;
String message;
List<Emp> empList;

@Override
public void prepare() throws Exception {
    emp = new Emp();
    message = "";
    empList = new ArrayList<Emp>();
    ServletContext ctx = ServletActionContext.getServletContext();
    WebApplicationContext webappCtx = WebApplicationContextUtils.getWebApplicationContext(ctx);
    empdao = (EmpDao)webappCtx.getBean("empDao");
}

@Override
public Emp getModel() {
    return emp;
}

public String searchEmpAction() throws Exception {
    System.out.println("In search emp action");
    System.out.println("Search employee : " + emp.getName());

    Emp emp2 = empdao.getEmployee(emp.getId());
    empList.add(emp2);
    System.out.println("In search emp action");
    System.out.println("Searched employee : " + emp2.getName());
    return SUCCESS;
}

public Emp getEmp() {
    return emp;
}

public void setEmp(Emp emp) {
    this.emp = emp;
}

public EmpDao getEmpdao() {
    return empdao;
}

public void setEmpdao(EmpDao empdao) {
    this.empdao = empdao;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public List<Emp> getEmpList() {
    return empList;
}

public void setEmpList(List<Emp> empList) {
    this.empList = empList;
}
}

Here is my dao class:

package com.rewardsapp.dao;

import org.jooq.DSLContext;
import org.springframework.dao.DataAccessException;

import com.rewardsapp.enums.ObjectType;
import com.rewardsapp.utils.NullObjectRegistry;

import static test.generated.tables.Emp.EMP;
import test.generated.tables.pojos.Emp;
import test.generated.tables.records.EmpRecord;

public class EmpDao {

/** The Constant NULL_CUSTOMER. */
private static final Emp NULL_EMP = (Emp)   NullObjectRegistry.getNullObject(ObjectType.EMP);

DSLContext dslContext;

public DSLContext getDslContext() {
    return dslContext;
}

public void setDslContext(DSLContext dslContext) {
    this.dslContext = dslContext;
}

public Emp getEmployee(int empId) throws Exception {
    try {
        EmpRecord empRecord = (EmpRecord) dslContext.select().from(EMP)
                .where((EMP.ID.equal(empId))).fetchAny();
        return (empRecord != null) ? empRecord.into(Emp.class) : NULL_EMP;
    } catch (DataAccessException e) {
        e.printStackTrace();
        /*throw new SystemException("Exception during getCustomerByCustomerType: " + customerType, e,
                SystemErrorCode.SQL_EXCEPTION_ERROR)*/;
        throw new Exception();
    }   
}
}

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="searchEmpAction" class="com.rewardsapp.action.EmpAction" method="searchEmpAction">
<result name="success">index.jsp</result>
<result name="input">index.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>
user2805924
  • 95
  • 1
  • 1
  • 10
  • show us your struts.xml..file – goodyzain Jul 21 '15 at 05:01
  • I have added struts.xml..Please have a look. Thanks in advance! – user2805924 Jul 21 '15 at 05:10
  • I tried that now. I get this error There is no Action mapped for namespace [/] and action name [EmpAction] associated with context path [/RewardsWebApp]. – user2805924 Jul 21 '15 at 05:37
  • remove Method attribute...and check this http://stackoverflow.com/questions/16872769/how-to-call-different-methods-defined-in-one-action-in-struts2 the second answer where you can call methods of Action class... – goodyzain Jul 21 '15 at 05:40
  • My method name is not "execute". So, I cannot remove the method name. Please correct me if I am wrong. – user2805924 Jul 21 '15 at 05:52
  • WARNING: No configuration found for the specified action: '/index.jsp' in names ace: ''. Form action defaulting to 'action' attribute's literal value. – user2805924 Jul 21 '15 at 05:52
  • I posted all the updated code above. Please have a look. Thanks. – user2805924 Jul 21 '15 at 06:20
  • searchEmpAction add in s:form action=searchEmpAction should work.. – goodyzain Jul 21 '15 at 06:59
  • But the form has various buttons for search, update etc. How would that be handled? – user2805924 Jul 21 '15 at 07:03
  • Need to have multiple form and multiple ActionName With OneAction...And Calling Methods...like this..http://stackoverflow.com/questions/16872769/how-to-call-different-methods-defined-in-one-action-in-struts2...is it invoking Action Now...?? – goodyzain Jul 21 '15 at 07:05
  • 1
    If you are using `2.3.15.3+` version of S2 then you need to set `struts.mapper.action.prefix.enabled` to use action attribute in submit tag. See http://stackoverflow.com/a/13343982/1700321. – Aleksandr M Jul 21 '15 at 09:05
  • Multiple forms and multiple actions, you should read http://stackoverflow.com/a/31495617/573032 – Roman C Jul 22 '15 at 00:26

0 Answers0