Last time, I tried to make a speech about how to design a MVC frame by oneself in our software club. However, Neo pointed out the
difference between MVC and MVP: in MVC, C will never know about the V and it will never modify the V.
Though I don’t think we should care much about whether webwork&struts are MVC or MVP frames,but at east ,MVC isfunny. Is it right?
And also, I have made my frame be more likely a MVC frame.
We will need two jsp files, a java class as model, and a java class as control.
Let’s come to model first:
The model is a simple javabean,in fact the base class Model have a field for view. But customer using it don’t have to know that,
they just need to define the fields which will be the data showing in the view.
//configurate the interceptors
@InterceptorsRef(interceptors = {ExceptionInterceptor.class ,ModelInterceptor.class})
//point out control for the model;
@ControlRef(clazz =UserControl.class)
public class UserModel extends Model
{
private User user = new User();//A javaBean User
private String people = “”;//people
public String getPeople()
{
return people;
}
public void setPeople(String people)
{
this.people = people;
}
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
}
next is the control class:
public class UserControl extends Control
{
private UserModel model;
private Logger log = Logger.getLogger(”debugLogger”);
public void setModel(Model model) //you should set the data model into the control;
{
this.model = (UserModel) model;
}
public void process()
{
User user = model.getUser(); //get the User from the model
Integer age = user.getUserAge();
if (age != null)
{
age += 100;
user.setUserAge(age);
}
user.setUserName(user.getUserName() + “+ processed”);
user.setUserTitle(user.getUserTitle() + “+ processed”);//change the value of User
model.setPeople(model.getPeople() + ” processed”);//change the value of people
model.setResult(Result.jsp(”result.jsp”));//change the result of the model
}
}
and then we will come to the view:
the input view:
<!–”UserModel” means the class name of the model, and “process” means which method of the control will be invoked –>
<form action=”UserModel-process.do” method=”post”>
<table width=”30%”>
<tr>
<td>user name:<input type=”text” name=”user.userName”></td>
</tr>
<tr>
<td>user age:<input type=”text” name=”user.userAge”></td>
</tr>
<tr>
<td>user title:<input type=”text” name=”user.userTitle”></td>
</tr>
<tr>
<td>people:<input type=”text” name=”people”></td>
</tr>
<tr>
<td>
<input type=”submit” value=”process”>
</td>
</tr>
</table>
</form>
and then it will come to the result view:
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<%@ taglib uri=”/WEB-INF/simple-property.tld” prefix=”simple” %>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>
<title>Simple jsp page</title></head>
<table>
<tr>
<td>user name:<br><font color=”green”><simple:property property=”user.userName”/></font></td>
</tr>
<tr>
<td>user age:<br><font color=”green”><simple:property property=”user.userAge”/></font></td>
</tr>
<tr>
<td>user title:<br><font color=”green”><simple:property property=”user.userTitle”/></font></td>
</tr>
<tr>
<td>people:<br><font color=”green”><simple:property property=”people”/></font></td>
</tr>
</table>
</html>
I don’t think this kind of frame is a real MVC frame: the control will never modify the data of the view. and the view will never know the
control.
And at least, we can easily tell that which’s the model,which’s the view,and which’s the control.
After I developed the frame, and tried to use it for sereval times. I found that, in fact, about struts:the model is the ActionForm,the control
is the Action, and the jsp files is the View. The difference is that, the model was put in the control,and the View directly specifies it’s control.
Its design make it more easily to be used,but less clearly to be understand.