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=?

  • 重定向和 转发
没有视图解析器
@RequestMapping("/m1/t1")
public String test(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.sendRedirect("WEB-INF/jsp/index.jsp");
    return "hello";
}

@RequestMapping("/m1/t2")
public String test2(Model model) {
    //默认为转发
    model.addAttribute("msg","z1");
    return "/hello.jsp";
}

@RequestMapping("/m1/t4")
public String test4(Model model) {
    //重定向
    model.addAttribute("msg","重定向");
    return "redirect:/hello.jsp";
}

有视图解析器


```java
@RequestMapping("/m1/t2")
public String test2(Model model) {
    //默认为转发
    model.addAttribute("msg","z1");
    return "/hello";
}

@RequestMapping("/m1/t4")
public String test4(Model model) {
    //重定向
    model.addAttribute("msg","重定向");
    return "redirect:/hello.jsp";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值