IDEA springmvc 开始项目

开始配置

-- pom.xml --




  4.0.0

  cn.wxy
  WXYProj
  1.0-SNAPSHOT
  war

  WXYProj Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    4.1.7.RELEASE
  

  
    
      junit
      junit
      4.11
      test
    

      
          junit
          junit
          3.8.1
          test
      

      
      
      
          org.springframework
          spring-core
          ${spring.version}
      

      
      
          org.springframework
          spring-beans
          ${spring.version}
      

      
          org.springframework
          spring-context
          ${spring.version}
      

      
          org.springframework
          spring-context-support
          ${spring.version}
      

      
          org.springframework
          spring-web
          ${spring.version}
      

      
          org.springframework
          spring-webmvc
          ${spring.version}
      

      
          org.springframework
          spring-tx
          ${spring.version}
      

      
          org.springframework
          spring-aop
          ${spring.version}
      

      
          org.springframework
          spring-jdbc
          ${spring.version}
      
    
          jstl
          jstl
          1.1.2
      
  

  
    WXYProj
    ...
  

-- WEB-INF/web.xml --




  Archetype Created Web Application
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      /WEB-INF/springmvc-config.xml
    
  
  
    springmvc
    /
  

-- WEB-INF/springmvc-config.xml --



    
        
        
        
        
    

这里指定的BeanNameUrlHandlerMapping, SimpleControllerHandlerAdapter, InternalResourceViewResolver分别是映射器(处理url->class),适配器(处理class中的方法)和jsp页面调用。这三个bean也是默认的,可以省略不写。

在IDEA中使用javax需要把tomcat中的servlet-api.jar, jsp-api.jsp加入到WEB-INF/lib下,如果没有lib,可以新建一个,加入这两个jar包后要右键add as Library.

-- src/main/java/cn.weizhen/HelloController.java --

package cn.weizhen;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController implements Controller {

    public static Log logger = LogFactory.getLog(HelloController.class);

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("Did called handleRequest");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("WEB-INF/content/welcome.jsp");
        modelAndView.addObject("message", "Hello world");
        return modelAndView;
    }
}

-- WEB-INF/content/welcome.jsp --

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>


    Title


Hello

${requestScope.message}

运行:浏览器键入:http://localhost:8080/hello
结果:
Hello
Hello world

上面的示例中,welcome.jsp使用了EL表达式,因此在pom.xml中导入了jstl的架包。而HelloController实现spring的Controller接口并实现其中的方法:ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception, 这是Spring2.5之前的方式,这种方式只能处理一个单一请求。而Spring2.5之后一般通过基于注解的方式处理,不需要实现任何接口,并且可以支持同时
处理多个请求动作。


使用注解

现在基本上都是使用注解的方式了。
修改springmvc-config.xml文件:



    
    
        
        
        
        
        
        
        
        
        
        
    

上面的映射器、适配器、解析器只是为了显示配置,可省略。
修改HelloController.java:

package cn.weizhen;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
    public static Log logger = LogFactory.getLog(HelloController.class);

    public HelloController() {
    }

    @RequestMapping({"/hello"})
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("Did called handleRequest");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("WEB-INF/content/welcome.jsp");
        modelAndView.addObject("message", "Hello world");
        return modelAndView;
    }
}

你可能感兴趣的:(IDEA springmvc 开始项目)