Spring MVC 注解配置

1、配置Web.xml 
Java代码   收藏代码
  1. <!-- Spring MVC Servlet -->  
  2.     <servlet>  
  3.          <servlet-name>springmvc</servlet-name>  
  4.          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  5.          <load-on-startup>2</load-on-startup>  
  6.     </servlet>  
  7.        
  8.     <servlet-mapping>  
  9.         <servlet-name>springmvc</servlet-name>  
  10.         <url-pattern>*.do</url-pattern>  
  11.     </servlet-mapping>  


2、配置ApplicationContext.xml 

Java代码   收藏代码
  1.       
  2. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
  3.      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false"/>  
  4.       
  5.           <!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖!-->  
  6.           <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  


3、Controller配置 

(1)、一个方法一个Action 
Java代码   收藏代码
  1. package com.dragonsoft.library.web.action.manager;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.ui.ModelMap;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. @Controller  
  8. public class MainRootController {  
  9.       
  10.     @RequestMapping("/manager/main.do")  
  11.     public String mainHandler(ModelMap model){  
  12.           
  13.         return "/manager/main";  
  14.     }  
  15.       
  16. }  


(2)、参数方法 
Java代码   收藏代码
  1. package com.dragonsoft.library.web.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Serializable;  
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. import org.slf4j.Logger;  
  8. import org.slf4j.LoggerFactory;  
  9. import org.springframework.ui.ModelMap;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12. import org.springframework.web.bind.annotation.SessionAttributes;  
  13.   
  14. @SessionAttributes(GenericController.SESSIONMESSAGE)  
  15. public abstract class GenericController<T, ID extends Serializable> {  
  16.       
  17.     protected Logger logger = LoggerFactory.getLogger(getClass());  
  18.       
  19.     public static final String FLASHMESSAGE = "flashMessage";//信息,重定向后消失  
  20.       
  21.     public static final String SESSIONMESSAGE = "sessionMessage";//会话信息  
  22.       
  23.     public static final String WARNMESSAGE = "warnMessage";//警告信息  
  24.           
  25.     protected static final String SHOW = "show";  
  26.       
  27.     protected static final String EDIT = "edit";  
  28.       
  29.     protected static final String CREATE = "create";  
  30.       
  31.     protected static final String LIST = "list";  
  32.       
  33.       
  34.     /** 
  35.      * 模板目录 
  36.      * @return 
  37.      */  
  38.     public String getBasePath(){  
  39.         return "";  
  40.     }  
  41.       
  42.     /** 
  43.      * 查看 
  44.      * @param model 
  45.      * @return 
  46.      */  
  47.     @RequestMapping(params = "method=show")  
  48.     public String showHandler(ID id, ModelMap model){  
  49.         return getBasePath()+show(id, model);  
  50.     }  
  51.       
  52.     public abstract String show(ID id, ModelMap model);  
  53.       
  54.     /** 
  55.      * 创建 
  56.      * @param model 
  57.      * @return 
  58.      */  
  59.     @RequestMapping(params = "method=create")  
  60.     public String createHandler(ModelMap model){  
  61.         return getBasePath()+create(model);  
  62.     }  
  63.       
  64.     public abstract String create(ModelMap model);  
  65.       
  66.     /** 
  67.      * 修改 
  68.      * @param model 
  69.      * @return 
  70.      */  
  71.     @RequestMapping(params = "method=edit")  
  72.     public String editHandler(ID id, ModelMap model){  
  73.         return getBasePath()+edit(id,model);  
  74.     }  
  75.       
  76.     public abstract String edit(ID id, ModelMap model);  
  77.       
  78.     /** 
  79.      * 列表 
  80.      * @param model 
  81.      * @return 
  82.      */  
  83.     @RequestMapping(params = "method=list")  
  84.     public String listHandler(ModelMap model){  
  85.         return getBasePath()+list(model);  
  86.     }  
  87.       
  88.     public abstract String list(ModelMap model);  
  89.       
  90.     /** 
  91.      * 保存 
  92.      * @param entity 
  93.      * @param model 
  94.      * @return 
  95.      */  
  96.     @RequestMapping(params = "method=save", method = RequestMethod.POST)  
  97.     public String saveHandler(T entity, ModelMap model){  
  98.         preSave(entity, model);  
  99.         return getBasePath()+save(entity, model);  
  100.     }  
  101.       
  102.     public void preSave(T entity, ModelMap model){  
  103.           
  104.     }  
  105.     public abstract String save(T entity, ModelMap model);  
  106.       
  107.     /** 
  108.      * 更新 
  109.      * @param entity 
  110.      * @param model 
  111.      * @return 
  112.      */  
  113.   
  114.     @RequestMapping(params = "method=update", method = RequestMethod.POST)  
  115.     public String updateHandler(T entity, ModelMap model){  
  116.         preUpdate(entity, model);  
  117.         return getBasePath()+update(entity, model);  
  118.     }  
  119.       
  120.     public void preUpdate(T entity, ModelMap model){  
  121.           
  122.     }  
  123.     public abstract String update(T entity, ModelMap model);  
  124.       
  125.     /** 
  126.      * 删除 
  127.      * @param id 
  128.      * @param model 
  129.      * @return 
  130.      */  
  131.     @RequestMapping(params = "method=delete")  
  132.     public String deleteHandler(ID id, ModelMap model){  
  133.         return getBasePath()+delete(id, model);  
  134.     }  
  135.       
  136.     public abstract String delete(ID id, ModelMap model);  
  137.       
  138.     /** 
  139.      * 信息 
  140.      * @param message 
  141.      * @param model 
  142.      */  
  143.     public void addFlashMessage(String message, ModelMap model){  
  144.         model.addAttribute(FLASHMESSAGE, message);  
  145.     }  
  146.       
  147.     public void addWarnMessage(String message, ModelMap model){  
  148.         model.addAttribute(WARNMESSAGE, message);  
  149.     }  
  150.       
  151.     public void addSessionMessage(String message, ModelMap model){  
  152.         model.addAttribute(SESSIONMESSAGE, message);  
  153.     }  
  154.       
  155.     /** 
  156.      * 绕过Template,直接输出内容的简便函数.  
  157.      */  
  158.     protected String render(String text, String contentType, HttpServletResponse response) {  
  159.         try {  
  160.             response.setContentType(contentType);  
  161.             response.getWriter().write(text);  
  162.         } catch (IOException e) {  
  163.             logger.error(e.getMessage(), e);  
  164.         }  
  165.         return null;  
  166.     }  
  167.   
  168.     /** 
  169.      * 直接输出字符串. 
  170.      */  
  171.     protected String renderText(String text, HttpServletResponse response) {  
  172.         return render(text, "text/plain;charset=UTF-8", response);  
  173.     }  
  174.   
  175.     /** 
  176.      * 直接输出HTML. 
  177.      */  
  178.     protected String renderHtml(String html, HttpServletResponse response) {  
  179.         return render(html, "text/html;charset=UTF-8", response);  
  180.     }  
  181.   
  182.     /** 
  183.      * 直接输出XML. 
  184.      */  
  185.     protected String renderXML(String xml, HttpServletResponse response) {  
  186.         return render(xml, "text/xml;charset=UTF-8", response);  
  187.     }  
  188.       
  189. }  



(3)、传递参数 

直接在方法中添加要传入的参数即可 

例: 
Java代码   收藏代码
  1.        @RequestMapping(params = "method=update", method = RequestMethod.POST)  
  2. public String updateHandler(T entity, ModelMap model){  
  3.     preUpdate(entity, model);  
  4.     return getBasePath()+update(entity, model);  
  5. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值