SpringMVC注解式开发

什么是注解?

注解就像修饰符一样,可以用于从java代码中抽取文档、跟踪代码中的依赖性或者在编译时做检查。注解可以被应用在包、类、方法、成员变量、参数和本地变量的声明中。我们大多数人最先接触的注解就是@Override。

注解的工作原理就是,先使用注解修饰java代码,然后另一块叫做注解处理器的代码会解析这段注解和被修饰的代码并做相应的处理。

SpringMVC怎么使用注解?

开发步骤如下:

1.实体类创建  

//让其成为一个控制器类
@Controller
/*@RequestMapping("/Logger")*/
public class AnnocationTest {
//这里的/dofirst就是用来在浏览器地址栏中输入的url @RequestMapping(
"/dofirst") public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { return "First"; } @RequestMapping("/dosecond") public String doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { return "Second"; } }

2.xml配置的书写

 //包扫描器
<context:component-scan base-package="day07"></context:component-scan>
//视图解析器 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>

3.web.xml配置

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMvcday07annocation.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

SprincMVC注解命名空间的使用:

作用:隔离相同名称的类/方法

例如:

//让其成为一个控制器类
@Controller
//命名空间  
@RequestMapping("/Logger")
public class AnnocationTest {
@RequestMapping("/dofirst")
    public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "First";
    }
    @RequestMapping("/dosecond")
    public String doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "Second";
    }
}

SpringMVC中通配符中的用法:

*代表0或者多个字符 在@RequestMapping中一共 有四种用法:并列举几个示例

1./*third

以任意字符开头,以third结尾

@RequestMapping("/*dofirst")
    public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "First";
    }

2./third*

以third开头,以任意字符结尾

   @RequestMapping("/dofirst*")
    public String doFirst0(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "First";
    }

3./**/third

在third前需要有多级路径,或者是没有路径,都可以访问

    @RequestMapping("/**/dofirst")
    public String doFirst1(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "First";
    }

4./*/third

在third之前,必须书写一级路径,而且必须是一级

  @RequestMapping("/*/dofirst")
    public String doFirst2(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "First";
    }

SpingMVC中请求方式的定义:

@RequestMapping有一个属性method,用于被注解方法所处理请求的提交方式进行限制,只有满足该Method属性限定的方式才会执行该方法

例如:

    @RequestMapping(value = "/*dofirst",method = RequestMethod.POST)
    public String doFirst3(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        return "First";
    }
原文地址:https://www.cnblogs.com/1234AAA/p/8640774.html