springmvc 学习2
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>-->
<!-- <!– 让过滤器不过滤静态资源–>-->
<!-- <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>
缺点:一个控制器只能写一种方法,如果需要写多个方法则需要写更多的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>
注解在方法上面的访问路径
http://localhost:8080/项目名/注解名 作用:用户模式
同时注解在类和方法上面的访问路径
http://localhost:8080/项目名/加在类上面的注解名/方法上的注解名 作用:管理员模式-
访问常见种类:
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";
}