1、配置Web.xml
2、配置ApplicationContext.xml
3、Controller配置
(1)、一个方法一个Action
(2)、参数方法
(3)、传递参数
直接在方法中添加要传入的参数即可
例:
- <!-- Spring MVC Servlet -->
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
2、配置ApplicationContext.xml
- <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false"/>
- <!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖!-->
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
3、Controller配置
(1)、一个方法一个Action
- package com.dragonsoft.library.web.action.manager;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class MainRootController {
- @RequestMapping("/manager/main.do")
- public String mainHandler(ModelMap model){
- return "/manager/main";
- }
- }
(2)、参数方法
- package com.dragonsoft.library.web.action;
- import java.io.IOException;
- import java.io.Serializable;
- import javax.servlet.http.HttpServletResponse;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.SessionAttributes;
- @SessionAttributes(GenericController.SESSIONMESSAGE)
- public abstract class GenericController<T, ID extends Serializable> {
- protected Logger logger = LoggerFactory.getLogger(getClass());
- public static final String FLASHMESSAGE = "flashMessage";//信息,重定向后消失
- public static final String SESSIONMESSAGE = "sessionMessage";//会话信息
- public static final String WARNMESSAGE = "warnMessage";//警告信息
- protected static final String SHOW = "show";
- protected static final String EDIT = "edit";
- protected static final String CREATE = "create";
- protected static final String LIST = "list";
- /**
- * 模板目录
- * @return
- */
- public String getBasePath(){
- return "";
- }
- /**
- * 查看
- * @param model
- * @return
- */
- @RequestMapping(params = "method=show")
- public String showHandler(ID id, ModelMap model){
- return getBasePath()+show(id, model);
- }
- public abstract String show(ID id, ModelMap model);
- /**
- * 创建
- * @param model
- * @return
- */
- @RequestMapping(params = "method=create")
- public String createHandler(ModelMap model){
- return getBasePath()+create(model);
- }
- public abstract String create(ModelMap model);
- /**
- * 修改
- * @param model
- * @return
- */
- @RequestMapping(params = "method=edit")
- public String editHandler(ID id, ModelMap model){
- return getBasePath()+edit(id,model);
- }
- public abstract String edit(ID id, ModelMap model);
- /**
- * 列表
- * @param model
- * @return
- */
- @RequestMapping(params = "method=list")
- public String listHandler(ModelMap model){
- return getBasePath()+list(model);
- }
- public abstract String list(ModelMap model);
- /**
- * 保存
- * @param entity
- * @param model
- * @return
- */
- @RequestMapping(params = "method=save", method = RequestMethod.POST)
- public String saveHandler(T entity, ModelMap model){
- preSave(entity, model);
- return getBasePath()+save(entity, model);
- }
- public void preSave(T entity, ModelMap model){
- }
- public abstract String save(T entity, ModelMap model);
- /**
- * 更新
- * @param entity
- * @param model
- * @return
- */
- @RequestMapping(params = "method=update", method = RequestMethod.POST)
- public String updateHandler(T entity, ModelMap model){
- preUpdate(entity, model);
- return getBasePath()+update(entity, model);
- }
- public void preUpdate(T entity, ModelMap model){
- }
- public abstract String update(T entity, ModelMap model);
- /**
- * 删除
- * @param id
- * @param model
- * @return
- */
- @RequestMapping(params = "method=delete")
- public String deleteHandler(ID id, ModelMap model){
- return getBasePath()+delete(id, model);
- }
- public abstract String delete(ID id, ModelMap model);
- /**
- * 信息
- * @param message
- * @param model
- */
- public void addFlashMessage(String message, ModelMap model){
- model.addAttribute(FLASHMESSAGE, message);
- }
- public void addWarnMessage(String message, ModelMap model){
- model.addAttribute(WARNMESSAGE, message);
- }
- public void addSessionMessage(String message, ModelMap model){
- model.addAttribute(SESSIONMESSAGE, message);
- }
- /**
- * 绕过Template,直接输出内容的简便函数.
- */
- protected String render(String text, String contentType, HttpServletResponse response) {
- try {
- response.setContentType(contentType);
- response.getWriter().write(text);
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- /**
- * 直接输出字符串.
- */
- protected String renderText(String text, HttpServletResponse response) {
- return render(text, "text/plain;charset=UTF-8", response);
- }
- /**
- * 直接输出HTML.
- */
- protected String renderHtml(String html, HttpServletResponse response) {
- return render(html, "text/html;charset=UTF-8", response);
- }
- /**
- * 直接输出XML.
- */
- protected String renderXML(String xml, HttpServletResponse response) {
- return render(xml, "text/xml;charset=UTF-8", response);
- }
- }
(3)、传递参数
直接在方法中添加要传入的参数即可
例:
- @RequestMapping(params = "method=update", method = RequestMethod.POST)
- public String updateHandler(T entity, ModelMap model){
- preUpdate(entity, model);
- return getBasePath()+update(entity, model);
- }