How to connect model view and controller using Spring

In a swing GUI application that uses the MVC pattern, how can we use Spring to connect the model view and controller? that is, what beans (model, view or controller) should be entered using Spring and what should be created from the application? When developing the application, I applied the MVC pattern here . Thanks in advance.

+4
source share
5 answers

I defined all the beans in spring and used the factory method to create views if necessary. The controller is introduced into the view, and the model and view are added to the controller through spring.

The following are code examples from a simple example that I came across to find a solution: (sorry for the long post!)

application context file:

<bean id="firstModel" class="com.model.FirstModel"></bean> <bean id="secondModel" class="com.model.SecondModel"></bean> <bean id="firstController" class="com.controller.FirstController" /> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="firstController" /> </property> <property name="targetMethod"> <value>addModel</value> </property> <property name="arguments"> <list> <value>FIRST</value> <ref local="firstModel" /> </list> </property> </bean> <bean id="secondController" class="com.controller.SecondController" /> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="secondController" /> </property> <property name="targetMethod"> <value>addModel</value> </property> <property name="arguments"> <list> <value>SECOND</value> <ref local="secondModel" /> </list> </property> </bean> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="secondController" /> </property> <property name="targetMethod"> <value>addModel</value> </property> <property name="arguments"> <list> <value>FIRST</value> <ref local="firstModel" /> </list> </property> </bean> <bean id="firstForm" class="com.view.FirstForm"> <property name="controller"> <ref bean="firstController" /> </property> </bean> <bean id="secondForm" class="com.view.SecondForm"> <property name="controller"> <ref bean="secondController" /> </property> </bean> 

The following is an abstract controller class:

 public class AbstractController implements PropertyChangeListener { Map<Type, BaseView> registeredViews; Map<Type, AbstractModel> registeredModels; public AbstractController() { registeredViews = new HashMap<Type, BaseView>(); registeredModels = new HashMap<Type, AbstractModel>(); } public void addModel(Type type, AbstractModel model) { registeredModels.put(type, model); model.addPropertyChangeListener(this); } public void removeModel(AbstractModel model) { registeredModels.remove(model); model.removePropertyChangeListener(this); } public void addView(BaseView view, Type type) { registeredViews.put(type, view); } public void removeView(javax.swing.JFrame view) { registeredViews.remove(view); } public void propertyChange(PropertyChangeEvent evt) { for (BaseView view : registeredViews.values()) { view.modelPropertyChange(evt); } } protected void setModelProperty(String propertyName, Object newValue) { for (AbstractModel model : registeredModels.values()) { Statement statment = new Statement(model, "set" + propertyName, new Object[] { newValue }); try { statment.execute(); } catch (NoSuchMethodException e) { continue; } catch (Exception e) { e.printStackTrace(); } } } } 

The following is an abstract model class:

 public class AbstractModel { protected PropertyChangeSupport propertyChangeSupport; public AbstractModel() { propertyChangeSupport = new PropertyChangeSupport(this); } public void addPropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(listener); } protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); } } 

The following is an example of interface code of the form:

 public interface BaseView { void modelPropertyChange(PropertyChangeEvent evt); public abstract void showForm(); } 

The following is a sample factory class code:

 public class FormFactory { private ApplicationContext context; private static FormFactory viewFactory; private FormFactory() { if (context == null) { context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); } } public static synchronized FormFactory getInstance() { if (viewFactory == null) { viewFactory = new FormFactory(); } return viewFactory; } public BaseView createForm(Type type) { BaseView form = null; switch (type) { case FIRST: form = (BaseView) context.getBean("firstForm"); break; case SECOND: form = (BaseView) context.getBean("secondForm"); break; default: break; } return form; } } 
0
source

If you have some freedom to use the technologies that you use, I would say that you switch to (Griffon) [http://griffon.codehaus.org/]. It uses spring in the background, and you also get the power of groovy and Swing UI builders. The best part is, you can still reuse the Java code you wrote. In addition, you do not need to worry about DI and other things. Griffon processes it for you.

+2
source

In one of my projects, I successfully used the Spring Rich Client .

If you start from scratch, I suggest you take a look at it, it's worth it. And it also provides some services out of the box (for example, authentication window, etc.).

+1
source

I suggest you use "spring mvc".

Jsp (View), how to display data;

The controller controls the return of the required viewing data;

Server controller - system logic;

A model is a database model.

0
source

No wonder I would recommend you take a look at Griffon. The MVC pattern is deeply rooted in Griffon DNA; look at this sample application as shown in the Griffin Handbook.

http://griffon.codehaus.org/guide/0.9.5-rc2/guide/2.%20Getting%20Started.html#2.3%20A%20Groovy%20Console%20Example

Griffon provides the basic DI features for each MVC member, you only need to define the properties according to the naming convention. Services in which you usually put most of the application logic are also automatically entered into the controllers, as explained in the manual

http://griffon.codehaus.org/guide/0.9.5-rc2/guide/8.%20Controllers%20and%20Services.html#8.2%20Services

However, you can also use Spring DI through the Spring plugin

http://artifacts.griffon-framework.org/plugin/spring

Spring beans can be defined using the standard XML approach, annotations, or Groovy Spring DSL.

0
source

Source: https://habr.com/ru/post/1403813/


All Articles