In a MVC application, where to perform request parameters data type conversion: Controller or Model?
For example I have 3 parameters coming with request: user_name, password, role_id. So I receive all 3 of them as string in the Controller. This is for example the Controller code:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
}
}
The 3rd parameter has to be treated as an integer. So I have 2 choices:
- convert it into an integer first in the Controller and then pass it to the Model.
In this case this is the Controller code:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
int roleIdN = Integer.parseInt(roleId);
MyService myService = new MyService();
boolean result = myService.login(userName, password, roleIdN);
...
}
}
And this is the Model code:
public class MyService {
public boolean login(String userName, String password, int roleId) {
...
}
}
- pass it to the Model as String; the Model will convert it into an integer before using it.
In this case this is the Controller code:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
MyService myService = new MyService();
boolean result = myService.login(userName, password, roleId);
...
}
}
And this is the Model code:
public class MyService {
public boolean login(String userName, String password, String roleId) {
int roleIdN = Integer.parseInt(roleId);
...
}
}
Where it makes the most sense? I am trying to figure out the boundaries of both Controller and Model. What if the 3rd parameter does not contain a number?
Aucun commentaire:
Enregistrer un commentaire