我刚刚在Ibm sprin mvc portlet上创建了一个小POC,它运行正常。但我有一个一直困扰着我的问题。在教程中它说我们可以拥有相同的DispatcherPortletor,我们可以使用自己的applicationContext和handler mapping来多个DispatcherPortlet。我的portlet.xml中有类似的东西:
<description>HelloWorld Portlet using Spring MVC portlet 1</description>
<portlet-name>HelloSpringPortletMVC1</portlet-name>
<display-name>Hello World Spring Portlet MVC Framework Portlet1</display-name>
<portlet-class> org.springframework.web.portlet.DispatcherPortlet </portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title> HelloSpringPortletMVC1 </title>
<short-title> HelloSpringPortletMVC1 </short-title>
<keywords> spring portlet </keywords>
</portlet-info>
<description>HelloWorld Portlet using Spring MVC portlet 2</description>
<portlet-name>HelloSpringPortletMVC2</portlet-name>
<display-name>Hello World Spring Portlet MVC Framework Portlet2</display-name>
<portlet-class> org.springframework.web.portlet.DispatcherPortlet </portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title> HelloSpringPortletMVC2 </title>
<short-title> HelloSpringPortletMVC2 </short-title>
<keywords> spring portlet </keywords>
</portlet-info>
我假设因为我们已经将portlet-class指定为org.springframework.web.portlet.DispatcherPortlet,每次呈现portlet时它都会创建一个DispatcherPortlet的新实例,那么我们怎么才能只有一个实例DispatcherPortlet将用于所有portlet。
答案 0 :(得分:1)
我假设因为我们已经将portlet-class指定为org.springframework.web.portlet.DispatcherPortlet,每次呈现portlet时它都会创建一个DispatcherPortlet的新实例,那么我们怎么才能只有一个实例将用于所有portlet的DispatcherPortlet
不需要创建多个DispatcherPortlet实例,您已经定义了将所有portlet请求路由到控制器的DispatcherPortlet,例如Spring MVC应用程序DispatcherServlet将您的请求转发给控制器。您需要的只是创建Controller类(类)并描述spring配置
portlet.xml的一部分
<portlet>
<portlet-name>My Portlet</portlet-name>
<display-name>My Portlet</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>classpath:/my_portlet.xml</value>
</init-param>
...
</portlet>
像这样的my_portlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd>
<context:annotation-config />
<context:component-scan base-package="my.package.portlet.controllers" />
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="myService" class="my.package.services.MyService"/>
</bean>
</beans>
控制器类是这样的(它可能包含错误,因为从内存中获取)
@Controller
@RequestMapping("view")
public class MyController {
@Autowired
private MyService myService;
@RequestMapping
public ModelAndView index(RenderRequest request, RenderResponse response) {
ModelAndView modelAndView = new ModelAndView("index.jsp");
modelAndView.addObject("attribute1", "Attribute 1 value");
modelAndView.addObject("attribute1", new Object());
return modelAndView;
}
@RenderMapping(params = "form=result")
public ModelAndView doSomething(RenderRequest request, RenderResponse response) {
ModelAndView modelAndView = new ModelAndView("result.jsp");
...
return modelAndView;
}
@ActionMapping(params = "submitSomething=yes")
public void submitSomething(@ModelAttribute("something") Something something, BindingResult bindingResult, ActionRequest request, ActionResponse response) {
// do what you want with form data
request.setAttribute("result", someResult);
response.setRenderParameter("form", "result");
}
}
您可以使用不同方法获得不同渲染/操作参数的多个Controller类的主要思想。也可以在控制器类中使用spring bean。