-1
\$\begingroup\$

I am trying to create an application where I need to know where to put the logic. Here is the code

HomeController.java

@Controller
public class HomeController {

    private EmployeeService employeeService;

    @Autowired(required=true)
    @Qualifier(value="employeeService")
    public void setEmployeeService(EmployeeService employeeService)
    {
        this.employeeService = employeeService;
    }

    @RequestMapping("/login")
    public ModelAndView loginWorld(@ModelAttribute("loginForm") Employee employee, ModelMap model){

        String result="", returnPage="";
        if(this.employeeService.validateLogin(employee.getUsername(), employee.getPassword())){
            result = "Welcome " + employee.getUsername();
            returnPage = "home";
        }
        else{
            result = "<BR>Login Failure";
            returnPage = "index";
        }
        model.addAttribute("result", result);
        return new ModelAndView(returnPage, model);
    }
}

EmployeeServiceImpl.java

@Service
public class EmployeeServiceImpl implements EmployeeService {

    /* (non-Javadoc)
     * @see com.smart.service.EmployeeService#validateLogin(java.lang.String, java.lang.String)
     */

    EmployeeDAO employeeDAO;

    public void setEmployeeDAO(EmployeeDAO employeeDAO)
    {
        this.employeeDAO = employeeDAO;
    }

    @Override
    public boolean validateLogin(String username, String password) {
        // TODO Auto-generated method stub
        return this.employeeDAO.validateLogin(username, password);

    }    
}

EmployeeDAOImpl.java

@Transactional(value="myTransactionManager")
public class EmployeeDaoImpl implements EmployeeDAO {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    @Override
    public boolean validateLogin(String username, String password) {

        return (long)this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM EmployeeInfo WHERE username = :username AND password = :password")
        .setString("username", username)
        .setString("password", password).uniqueResult() > 0;

    }    
}

I would like to know where to put the logic of the code, i.e. in Controller class, Service class or in the model class.

In the above code, I put validateLogin() to validate user in the controller class. I also want to put some more logic like checking if the data got from the textbox is null or If the user trying to login already logged in i.e. session already exist.

In which file do I put these logics?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

All the logic, in your case should be in the service class. The spring framework supports the MVC pattern . The controller essentially validates the input and sends it to the service class for the processing. The service class is the place where all the functionality is implemented and the Model class is more of interaction with database.

You can check for the validity of the input and if the session already exists in the service layer. Refer to this link for understanding more about Spring framework :http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.