赞
踩
1 | package org.springframework.web.servlet.mvc; |
2 | import javax.servlet.http.HttpServletRequest; |
3 | import javax.servlet.http.HttpServletResponse; |
4 | import org.springframework.web.servlet.ModelAndView; |
5 | public interface Controller { |
6 | ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception; |
7 | } |
01 | package org.springframework.web.servlet.mvc; |
02 | import javax.servlet.http.HttpServletRequest; |
03 | import javax.servlet.http.HttpServletResponse; |
04 | import javax.servlet.http.HttpSession; |
05 | import org.springframework.web.servlet.ModelAndView; |
06 | import org.springframework.web.servlet.support.WebContentGenerator; |
07 | import org.springframework.web.util.WebUtils; |
08 | public abstract class AbstractController extends WebContentGenerator implements Controller { |
09 | private boolean synchronizeOnSession = false ; |
10 | public final void setSynchronizeOnSession( boolean synchronizeOnSession) { |
11 | this .synchronizeOnSession = synchronizeOnSession; |
12 | } |
13 | public final boolean isSynchronizeOnSession() { |
14 | return synchronizeOnSession; |
15 | } |
16 | public final ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) |
17 | throws Exception { |
18 | checkAndPrepare(request, response, this instanceof LastModified); |
19 | if ( this .synchronizeOnSession) { |
20 | HttpSession session = request.getSession( false ); |
21 | if (session != null ) { |
22 | Object mutex = WebUtils.getSessionMutex(session); |
23 | synchronized (mutex) { |
24 | return handleRequestInternal(request, response); |
25 | } |
26 | } |
27 | } |
28 | |
29 | return handleRequestInternal(request, response); |
30 | } |
31 | protected abstract ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) |
32 | throws Exception; |
33 | } |
01 | package samples; |
02 | public class SampleController extends AbstractController { |
03 | public ModelAndView handleRequestInternal( |
04 | HttpServletRequest request, |
05 | HttpServletResponse response) throws Exception { |
06 | ModelAndView modelAndView = new ModelAndView( "hello" ); |
07 | modelAndView.addObject( "message" , "Hello World!" ); |
08 | return modelAndView; |
09 | } |
10 | } |
1 | < bean id = "sampleController" class = "samples.SampleController" > |
2 | < property name = "cacheSeconds" value = "120" /> |
3 | </ bean > |
01 | <? xml version = "1.0" encoding = "ISO-8859-1" ?> |
02 | < web-app xmlns = "http://java.sun.com/xml/ns/j2ee" |
03 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 | xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" |
05 | version = "2.5" > |
06 | < servlet > |
07 | < servlet-name >dispatcherServlet</ servlet-name > |
08 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
09 | < init-param > |
10 | < param-name >contextConfigLocation</ param-name > |
11 | < param-value >/WEB-INF/dispatcherServlet-servlet.xml</ param-value > |
12 | </ init-param > |
13 | < load-on-startup >1</ load-on-startup > |
14 | </ servlet > |
15 | < servlet-mapping > |
16 | < servlet-name >dispatcherServlet</ servlet-name > |
17 | < url-pattern >*.do</ url-pattern > |
18 | </ servlet-mapping > |
19 | </ web-app > |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" |
03 | "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> |
04 | < beans > |
05 | < bean id = "urlMapping" class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > |
06 | < property name = "mappings" > |
07 | < props > |
08 | < prop key = "sample.do" >sampleMultiActionController</ prop > |
09 | </ props > |
10 | </ property > |
11 | </ bean > |
12 | < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > |
13 | < property name = "viewClass" > |
14 | < value >org.springframework.web.servlet.view.InternalResourceView</ value > |
15 | </ property > |
16 | < property name = "prefix" > |
17 | < value >/WEB-INF/jsp/</ value > |
18 | </ property > |
19 | < property name = "suffix" > |
20 | < value >.jsp</ value > |
21 | </ property > |
22 | </ bean > |
23 | |
24 | |
25 | < bean id = "sampleMultiActionController" class = "com.test.SampleMultiMActionController" > |
26 | < property name = "methodNameResolver" > |
27 | < ref bean = "paraMethodResolver" /> |
28 | </ property > |
29 | <!--viewName属性将依赖注入sampleMultiActionController类--> |
30 | < property name = "viewName" > |
31 | < value >showme</ value > |
32 | </ property > |
33 | </ bean > |
34 | |
35 | < bean id = "paraMethodResolver" class = "org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" > |
36 | < property name = "paramName" value = "whichMethod" /> |
37 | </ bean > |
38 | </ beans > |
01 | import javax.servlet.http.HttpServletRequest; |
02 | import javax.servlet.http.HttpServletResponse; |
03 | import org.springframework.web.servlet.ModelAndView; |
04 | import org.springframework.web.servlet.mvc.Controller; |
05 | import java.io.IOException; |
06 | import java.util.*; |
07 | import javax.servlet.ServletException; |
08 | import org.apache.log4j.Logger; |
09 | import org.springframework.web.bind.*; |
10 | import org.springframework.web.servlet.ModelAndView; |
11 | import org.springframework.web.servlet.mvc.Controller; |
12 | org.springframework.web.servlet.mvc.multiaction.MultiActionController; |
13 | public class SampleMultiActionController extends MultiActionController { |
14 | private Logger logger=Logger.getLogger( this .getClass().getName()); |
15 | private String viewName; |
16 | //依赖注入一个名为viewName的参数,例如一个JSP文件,作为展示model的视图 |
17 | public String getViewName (){ |
18 | return this .viewName; |
19 | } |
20 | public void setViewName (String viewName){ |
21 | this . viewName =viewName; |
22 | } |
23 | |
24 | |
25 | public ModelAndView insert(HttpServletRequest req, |
26 | HttpServletResponse res) throws ServletRequestBindingException, IOException { |
27 | Map model = new HashMap(); |
28 | model.put( "dataList" , "新增数据..." ); |
29 | return new ModelAndView(getViewName(),model); |
30 | } |
31 | |
32 | public ModelAndView update(HttpServletRequest req, |
33 | HttpServletResponse res) throws ServletRequestBindingException, IOException { |
34 | Map model = new HashMap(); |
35 | model.put( "dataList" , "修改数据..." ); |
36 | return new ModelAndView(getViewName(),model); |
37 | } |
38 | |
39 | public ModelAndView delete(HttpServletRequest req, |
40 | HttpServletResponse res) throws ServletRequestBindingException, IOException { |
41 | Map model = new HashMap(); |
42 | model.put( "dataList" , "删除数据..." ); |
43 | return new ModelAndView(getViewName(),model); |
44 | } |
45 | } |
01 | <%@page c%> |
02 | <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> |
03 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> |
04 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
05 | < html > |
06 | < head >MuiltiActionController示例</ head > |
07 | < body > |
08 | < c:out value = "${model.dataList}" /> |
09 | </ body > |
10 | </ html > |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。