• Spring MVC 配置


    本文参考了,下面文章。
    http://www.cnblogs.com/superjt/p/3309255.html

    Web.xml:


    <!-- Spring MVC 总servlet配置 -->
    
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>&nbsp; 默认
        </init-param>
        -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- Spring Context 配置 -->
    <!-- ====================================== -->
    <listener>
       <listenerclass>
         org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
    
    
    <!--Spring Context 配置文件所在目录, 会被上面的Listener使用。默认配置在WEB-INF目录下 --> 
    
    <context-param> 
       <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/applicationContext.xml</param-value> 
    </context-param>

    spring-servlet.xml
    1. 此名称为不指定配置文件的默认名,
    2. 还可以在web.XML总配置,
    3. 也可以是
    DispatcherServlet 的名字开头的一个文件名。


    <?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:p="http://www.springframework.org/schema/p"     
            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.0.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
           http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>">
    
        <!-- 启用spring mvc 注解 -->
        <context:annotation-config />
    
        <!-- 设置使用注解的类所在的jar包 -->
        <context:component-scan base-package="controller"></context:component-scan>
    
        <!-- 完成请求和注解POJO的映射 -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
      
        <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
    </beans>



    Controller

    Controller.java
    @Controller
    @RequestMapping("/test2/login.do")可以作用在某个方法上。 
    @RequestMapping("/test2/login.do")可以作用在某个controller上, 函数上的URL会被追加到此URL后面
    
    @RequestMapping 如果在方法上且不指定详细URL,就是controller的默认处理方法。
    
    @RequestMapping(params = "method=1", method=RequestMethod.POST) URL参数
    @RequestMapping("/test3/*")通配符
    
    @RequestMapping(value="/comment/{blogId}", method=RequestMethod.POST) 路径参数化
    public void comment(Comment comment,@PathVariable int blogId, HttpSession session, HttpServletResponse response) throws IOException {
        
    }


    Spring MVC 参数:

    可以是单个参数
    可以是request/response,
    可以是JAVA POJO,
    可以指定是否为null,

    如果是post JSON对象,必须用request的流来出去并且处理。 返回要用@ResponseBody来指定。

    可以路径参数化
    public ModelAndView testLogin2(String username, String password, int age)
    
    public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request)
    public ModelAndView testLogin3(User user)
    @RequestMapping(value="/comment/{blogId}", method=RequestMethod.POST) 路径参数化
    public void comment(Comment comment,@PathVariable int blogId, HttpSession session, HttpServletResponse response) throws IOException {
        
    }



    返回值
    return "loginSuccess";//直接返回路径
    return new ModelAndView("loginSuccess");//返回modelviever
    
    mv=  new ModelAndView();
    mv.add
    return mv               //返回modelviever带数据
    
    
    Spring 相关
    @Resource(name = "loginService") //JAVA 标准的注解, 和@AutoWire一个功能。
    This code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.

    
    
  • 相关阅读:
    【Windows】Windows服务管家婆之Service Control Manager
    【Python】python文件名和文件路径操作
    【Python】python 调用c语言函数
    IIS 之 HTTP Error 404.2 – Not Found(ISAPI 和 CGI 限制)
    IIS 之 HTTP错误 404.17
    IIS 之 HTTP 错误 404.3
    IIS 之 HTTP 错误 403.14
    IIS 之 打开/关闭 Internet 信息服务
    Wcf 之 配置文件解析
    Web Service 之 开发、部署
  • 原文地址:https://www.cnblogs.com/zhuchunxu/p/spring_mvc.html
Copyright © 2020-2023  润新知