springMVC获取
概述
SpringMVC 是一种基于 Java 的实现 MVC 设计模式的轻量级 Web 框架SpringMVC 已经成为目前最主流的MVC框架之一,并且随着Spring3.0 的发布,全面超越 Struts2,成为最优秀的MVC 框架。它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实现任何接口。同时它还支持
RESTful 编程风格的请求。
SpringMVC的框架就是封装了原来Servlet中的共有行为;例如:参数封装,视图转发等。
作用:
符合mvc设计思想,在servlet的基础上,简化代码,提高开发效率。
使用springmvc的基本步骤
1.导入jar包
servlet-api.jar
spring-webmvc.jar
fastjson.jar
在pom.xml中导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
可以看到jar包都没有再项目中,一定要导入到项目中
一定要put into output root,因为jar包在本地仓库中存着,根本没有存在项目的根目录下
踩过这上面的坑所以一定要把包放进项目中
2.创建springmvc配置文件,在配置文件中开启注解模式,定义注解扫描的包
一般命名为spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--定义注解扫描的包-->
<context:component-scan base-package="com.lanou"></context:component-scan>
</beans>
3.在web.xml文件中配置
首先加入web支持项目右键—>Add Frameworks Support —>勾上Web Application
<?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>webmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定springmvc配置文件位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4 自定义controller
@Controller
public class UserControlller {
@RequestMapping("/find")
public ModelAndView find() {
System.out.println("---进入这个方法---");
ModelAndView mv = new ModelAndView();
mv.setViewName("index.jsp");
return mv;
}
}
在地址栏中输入/find,就会看到执行了这个方法
获取参数
在web下创建一个jsp文件夹,创建一个jsp文件
在xml中配置内部视图解析器
<!--内部视图资源解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
一、通过参数名称直接获取
一、参数名称一致,自动接收
http://localhost:8080/save?username=无情&password=123456&id=001
@RequestMapping("/save")
public ModelAndView save(String username,String password, Integer id){
System.out.println(username+"----"+password+"----"+id);
ModelAndView mv = new ModelAndView();
mv.setViewName("index02");
return mv;
}
二、 如果参数名称不一致
比如:http://localhost:8080/save?uname=无情&password=123456&id=001
@RequestMapping("/save")
public ModelAndView save(@RequestParam(value = "uname",required = false)String username,String password, Integer id){
System.out.println(username+"----"+password+"----"+id);
ModelAndView mv = new ModelAndView();
mv.setViewName("index02");
return mv;
}
required是否为必须的可以给值也可以不给值,false不是必须的true必须的
三、如果不输入密码呢?
比如: http://localhost:8080/save?uname=无情&id=1001
@RequestMapping("/save")
public ModelAndView save(@RequestParam(value = "uname",required = false) String username,
@RequestParam(value = "password",defaultValue = "66666") String password, Integer id){
System.out.println(username+"----"+password+"----"+id);
ModelAndView mv = new ModelAndView();
mv.setViewName("index02");
return mv;
}
二、通过对象接收
创建对象user
public class User {
private Integer id;
private String username;
private String password;
//...
}
通过对象获取参数
http://localhost:8080/update?uname=无情&password=123456&id=001
@RequestMapping("/update")
public ModelAndView update(User user){
System.out.println(user.toString());
//返回到具体的页面
ModelAndView mv = new ModelAndView();
mv.setViewName("index02");
return mv;
}
三、通过原生servlet接收
http://localhost:8080/delete?id=1
@RequestMapping("delete")
public ModelAndView delete(HttpServletRequest request){
String id = request.getParameter("id");
System.out.println(id);
//返回到具体的页面
ModelAndView mv = new ModelAndView();
mv.setViewName("index02");
return mv;
}
四、通过restful风格获取
http://localhost:8080/find/无情/1001
@RequestMapping("/find/{username}/{id}")
public ModelAndView find(@PathVariable("username") String usename, @PathVariable("id") Integer id){
System.out.println(usename+"----"+id);
//返回具体页面
ModelAndView mv = new ModelAndView();
mv.setViewName("index02");
return mv;
}
跳转页面
三种形式
一、转发
1.转发到指定的页面
@RequestMapping("/toRedirect")
public ModelAndView toRedirect(ModelAndView mv){
mv.setViewName("index02");
//转发到指定页面
//ModelAndView mv = new ModelAndView("index.jsp");
return mv;
}
2.转发到其他的控制层方法
@RequestMapping("/toRedirect")
public ModelAndView toRedirect(ModelAndView mv){
//mv.setViewName("/jsp/user.jsp");
//内部资源视图解析器
//mv.setViewName("user");
//转发到指定页面
//ModelAndView mv = new ModelAndView("index02");
//转发到save方法 其他的controller方法
mv.setViewName("forward:/save?uname=zhangsan&password=123&id=3");
return mv;
}
二、重定向
1.重定向到页面
@RequestMapping("/toRedirect2")
public ModelAndView toRedirect2(ModelAndView mv){
//重定向到页面
mv.setViewName("redirect:/jsp/user.jsp");
return mv;
}
2.重定向到其他的controller
@RequestMapping("/toRedirect2")
public ModelAndView toRedirect2(ModelAndView mv){
//重定向到页面
//mv.setViewName("redirect:/jsp/user.jsp");
//重定向到其他的controller
mv.setViewName("redirect:/save?uname=zja&password=123&id=0091");
return mv;
}
三、 返回json数据
可以跳转页面,也可以不跳转页面
@ResponseBody 返回json数据,不跳转页面
@RequestMapping("/getJson")
@ResponseBody
public String getJson(){
User user = new User();
user.setId(1233);
user.setUsername("zhangs");
user.setPassword("13354");
return JSON.toJSONString(user);
}
防止方法重名
比如两个controller类中都由save方法,这时候在浏览器中地址栏中输入/save路径就会报错。
一般会在一个controller类上添加一个
@RequestMapping(“user”)
这样之后在浏览器中输入的路径就为http://localhost:8080/user/(某某)这样就不会报错了
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("/save")
public ModelAndView save(@RequestParam(value = "uname",required = false) String username,
@RequestParam(value = "password",defaultValue = "66666") String password, Integer id){
//...
}
}
@Controller
@RequestMapping("arAcount")
public class ArAcount {
@RequestMapping("/save")
public ModelAndView addArAccount(@RequestParam(required = false) Integer id, String username, Double money, ModelAndView modelAndView){
//...
}
}