微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

springmvc 学习2

springmvc 学习2

  • controller实现方式一:实现controller接口

controller

public class ControllerTest01 implements Controller {
    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.addobject("msg","wodiaonimade");
        mv.setViewName("test");
        return mv;
    }
}

servletmvc-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--    扫描包后就可以使用注解-->
<!--    <context:component-scan base-package="cn.xiong.controller"></context:component-scan>-->
<!--    &lt;!&ndash;    让过滤器不过滤静态资源&ndash;&gt;-->
<!--    <mvc:default-servlet-handler/>-->
<!--    &lt;!&ndash;替代最开始的两个驱动类&ndash;&gt;-->
<!--    <mvc:annotation-driven/>-->
    <!--视图解析器:dispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean id="/t" class="cn.xiong.controller.ControllerTest01"></bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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:springmvc-servlet.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>
</web-app>

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

缺点:一个控制器只能写一种方法,如果需要写多个方法则需要写更多的controller类

  • controller实现方式二:使用注解

controller

@Controller
public class ControllerTest01{
    //请求的地址
    @RequestMapping(name = "/t")
    public String test(Model model){
        model.addAttribute("msg","wodiaonimade");
        return "test";
    }
}

servletmvc-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!--    扫描包后就可以使用注解-->
    <context:component-scan base-package="cn.xiong.controller"></context:component-scan>
    <!--    让过滤器不过滤静态资源-->
    <mvc:default-servlet-handler/>
    <!--替代最开始的两个驱动类-->
    <mvc:annotation-driven/>
    <!--视图解析器:dispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean id="/t" class="cn.xiong.controller.ControllerTest01"></bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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:springmvc-servlet.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>
</web-app>

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>
  • @RequestMapping:该注解用于映射url到控制器类或一个特定的处理程序方法,可用于类上或者用于方法上,用于类上的话则为用于方法上的父类,表示类中的所有的响应请求的方法都是该地址的父路径

注解在方法上面的访问路径

http://localhost:8080/项目名/注解名 作用:用户模式

同时注解在类和方法上面的访问路径

http://localhost:8080/项目名/加在类上面的注解名/方法上的注解名 作用:管理员模式-

  • RestFul风格:改变url的形式,易于缓存等功能的实现

访问常见种类:

post 对应mapper @PostMapping

get 对应mapper @GetMapping @RequestMapping(name="/haha/{a}/{b}",method=RequestMethod.GET)

put 对应mapper @PutMapping

delete 对应mapper @DeleteMapping

**:地址栏相同的情况下可以通过不同的提交访问方式实现不同的效果功能就会不同

@GetMapping("/haha/{a}/{b}") /haha/1/2 提交方式为get

@PostMapping("/haha/{a}/{b}") /haha/1/2 提交方式为post

@Controller
public class ControllerTest01{
    //请求的地址
    @RequestMapping("/haha/{a}/{b}")
    public String test(@PathVariable int a,@PathVariable int b, Model model){
        int ab=a+b;
        model.addAttribute("msg","wodiaonimade"+ab);
        return "test";
    }
}

访问路径为http://localhost:8080/项目名/注解名/?/?

@Controller
public class ControllerTest01{
    //请求的地址
 @RequestMapping(name="/haha/{a}/{b}",method=RequestMethod.GET)
    public String test(@PathVariable int a,@PathVariable int b, Model model){
        int ab=a+b;
        model.addAttribute("msg","wodiaonimade"+ab);
        return "test";
    }
}
-------------------------------------------
简化版本
@Controller
public class ControllerTest01{
    //请求的地址
    @GetMapping("/haha/{a}/{b}")
    public String test(@PathVariable int a,@PathVariable int b, Model model){
        int ab=a+b;
        model.addAttribute("msg","wodiaonimade"+ab);
        return "test";
    }
}

原始风格

@Controller
public class ControllerTest01{
    //请求的地址
    @RequestMapping("/haha")
    public String test(int a,int b, Model model){
        int ab=a+b;
        model.addAttribute("msg","wodiaonimade"+ab);
        return "test";
    }
}

原始访问路径为http://localhost:8080/项目名/注解名/a=?/b=?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐