Spring framework กับ Multi Action Controller
บทความเกี่ยวกับ : Spring framework กับ Multi Action Controller
มาดูตัวอย่างการสร้าง Multi Action กับ Spring กัน นะครับ
Controller Class
package com.en.controller;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.en.model.MasterModel;
public class MasterController extends MultiActionController{
protected MasterModel model;
protected String defaultScreen;
public MasterModel getModel() {
return model;
}
public void setModel(MasterModel model) {
this.model = model;
}
public String getDefaultScreen() {
return defaultScreen;
}
public void setDefaultScreen(String defaultScreen) {
this.defaultScreen = defaultScreen;
}
}
package com.en.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.en.dto.ExampleDto;
import com.en.model.ExampleModel;
public class ExampleController extends MasterController{
public ModelAndView loadMethod(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
String pk=request.getParameter("code_id");
ExampleDto dto=(ExampleDto)this.getModel().getDataObj(new Integer(pk));
mv.addObject("dto", dto);
mv.addObject("action","load");
return mv;
}
public ModelAndView insertMethod(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv=null;
try {
mv = new ModelAndView(this.getDefaultScreen());
String pk="0";
String tdesc=request.getParameter("tdesc");
String edesc=request.getParameter("edesc");
ExampleDto dto=new ExampleDto();
dto.setCodeId(new Integer(pk));
dto.setTdesc(tdesc);
dto.setEdesc(edesc);
this.getModel().insertData(dto);
mv.addObject("dto", dto);
mv.addObject("action","insert");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mv;
}
public ModelAndView deleteMethod(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
String pk=request.getParameter("code_id");
ExampleDto dto=new ExampleDto();
dto.setCodeId(new Integer(pk));
this.getModel().deleteData(dto);
mv.addObject("action","delete");
return mv;
}
public ModelAndView callWS(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
String pk=request.getParameter("code_id");
ExampleModel model=(ExampleModel)this.model;
ExampleDto dto=(ExampleDto)model.getDataWS(pk);
mv.addObject("dto",dto);
mv.addObject("action","ws");
return mv;
}
public ModelAndView callSAP(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
ExampleModel model=(ExampleModel)this.model;
model.callSAPDummy1();
mv.addObject("action","sap");
return mv;
}
}
Controller สร้างไปเรื่อยๆ ตาม Action ที่มีเลยนะครับกำหนด Method ได้ตามใจฉันเลยทีเดียว
จากนั้นมาทำการสร้าง Spring Config ตามนี้เลย
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/EnterprisePT/conf.properties"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref local="exampleInterceptor"/>
</list>
</property>
<property name="mappings">
<props>
<prop key="/example_insert.html">exampleController</prop>
<prop key="/example_load.html">exampleController</prop>
<prop key="/example_delete.html">exampleController</prop>
<prop key="/example_ws.html">exampleController</prop>
<prop key="/example_sap.html">exampleController</prop>
</props>
</property>
</bean>
<!-- ========================= CONTROLLER DEFINITIONS ========================= -->
<bean id="exampleController" class="com.en.controller.ExampleController">
<property name="methodNameResolver" ref="exampleControllerResolver"/>
<property name="model" ref="exampleModel"/>
<property name="defaultScreen" value="example"/>
</bean>
<bean id="exampleControllerResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/example_insert.html">insertMethod</prop>
<prop key="/example_load.html">loadMethod</prop>
<prop key="/example_delete.html">deleteMethod</prop>
<prop key="/example_ws.html">callWS</prop>
<prop key="/example_sap.html">callSAP</prop>
</props>
</property>
</bean>
<bean id="exampleModel" class="com.en.model.ExampleModel">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
<property name="wsClient" ref="exampleWSClient"/>
<property name="sap" ref="exampleSAP"/>
</bean>
<bean id="exampleInterceptor" class="com.en.interceptor.ExampleInterceptor" />
<bean id="exampleWSClient" class="com.en.ws.client.ExampleWSClient">
<property name="endpoint" value="http://127.0.0.1:8080/EnterPrisePT/ws/example"/>
</bean>
<bean id="exampleSAP" class="com.en.sap.ExampleSAP" />
</beans>
ตามนั้นเลยครับอยากรู้อะไรเพิ่มเติมสอบถามได้
มาดูตัวอย่างการสร้าง Multi Action กับ Spring กัน นะครับ
Controller Class
package com.en.controller;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.en.model.MasterModel;
public class MasterController extends MultiActionController{
protected MasterModel model;
protected String defaultScreen;
public MasterModel getModel() {
return model;
}
public void setModel(MasterModel model) {
this.model = model;
}
public String getDefaultScreen() {
return defaultScreen;
}
public void setDefaultScreen(String defaultScreen) {
this.defaultScreen = defaultScreen;
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.en.dto.ExampleDto;
import com.en.model.ExampleModel;
public class ExampleController extends MasterController{
public ModelAndView loadMethod(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
String pk=request.getParameter("code_id");
ExampleDto dto=(ExampleDto)this.getModel().getDataObj(new Integer(pk));
mv.addObject("dto", dto);
mv.addObject("action","load");
return mv;
}
public ModelAndView insertMethod(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv=null;
try {
mv = new ModelAndView(this.getDefaultScreen());
String pk="0";
String tdesc=request.getParameter("tdesc");
String edesc=request.getParameter("edesc");
ExampleDto dto=new ExampleDto();
dto.setCodeId(new Integer(pk));
dto.setTdesc(tdesc);
dto.setEdesc(edesc);
this.getModel().insertData(dto);
mv.addObject("dto", dto);
mv.addObject("action","insert");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mv;
}
public ModelAndView deleteMethod(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
String pk=request.getParameter("code_id");
ExampleDto dto=new ExampleDto();
dto.setCodeId(new Integer(pk));
this.getModel().deleteData(dto);
mv.addObject("action","delete");
return mv;
}
public ModelAndView callWS(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
String pk=request.getParameter("code_id");
ExampleModel model=(ExampleModel)this.model;
ExampleDto dto=(ExampleDto)model.getDataWS(pk);
mv.addObject("dto",dto);
mv.addObject("action","ws");
return mv;
}
public ModelAndView callSAP(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(this.getDefaultScreen());
ExampleModel model=(ExampleModel)this.model;
model.callSAPDummy1();
mv.addObject("action","sap");
return mv;
}
}
Controller สร้างไปเรื่อยๆ ตาม Action ที่มีเลยนะครับกำหนด Method ได้ตามใจฉันเลยทีเดียว
จากนั้นมาทำการสร้าง Spring Config ตามนี้เลย
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/EnterprisePT/conf.properties"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html; charset=UTF-8" />
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref local="exampleInterceptor"/>
</list>
</property>
<property name="mappings">
<props>
<prop key="/example_insert.html">exampleController</prop>
<prop key="/example_load.html">exampleController</prop>
<prop key="/example_delete.html">exampleController</prop>
<prop key="/example_ws.html">exampleController</prop>
<prop key="/example_sap.html">exampleController</prop>
</props>
</property>
</bean>
<!-- ========================= CONTROLLER DEFINITIONS ========================= -->
<bean id="exampleController" class="com.en.controller.ExampleController">
<property name="methodNameResolver" ref="exampleControllerResolver"/>
<property name="model" ref="exampleModel"/>
<property name="defaultScreen" value="example"/>
</bean>
<bean id="exampleControllerResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/example_insert.html">insertMethod</prop>
<prop key="/example_load.html">loadMethod</prop>
<prop key="/example_delete.html">deleteMethod</prop>
<prop key="/example_ws.html">callWS</prop>
<prop key="/example_sap.html">callSAP</prop>
</props>
</property>
</bean>
<bean id="exampleModel" class="com.en.model.ExampleModel">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
<property name="wsClient" ref="exampleWSClient"/>
<property name="sap" ref="exampleSAP"/>
</bean>
<bean id="exampleInterceptor" class="com.en.interceptor.ExampleInterceptor" />
<bean id="exampleWSClient" class="com.en.ws.client.ExampleWSClient">
<property name="endpoint" value="http://127.0.0.1:8080/EnterPrisePT/ws/example"/>
</bean>
<bean id="exampleSAP" class="com.en.sap.ExampleSAP" />
</beans>
ตามนั้นเลยครับอยากรู้อะไรเพิ่มเติมสอบถามได้
ความคิดเห็น
แสดงความคิดเห็น