先在一个包中创建一个类,然后再配置sprringmvacontroller.xml并链接到该类。
<context:component-scan base-package="cn.happy.controllerreturn"></context:component-scan>注解开发所创建的类及其方法
通配符:是一种符号,不是精确匹配,而是用来代替
** 代表任意级别目录,或者没有目录
package cn.happy.Controler; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by java on 2017/8/18. */ @Controller @RequestMapping("/user") public class HappyController { @RequestMapping("/first") public String doFirst(){ return "/WEB-INF/index.jsp"; } @RequestMapping("/second") public String doSecond(){ return "/WEB-INF/insert.jsp"; } @RequestMapping("/*thtird") public String doThird(){ return "/WEB-INF/delete.jsp"; } @RequestMapping("/**/fourth") public String doFourth(){ return "/WEB-INF/insert.jsp"; } @RequestMapping("/*/six") public String doSix(){ return "/WEB-INF/insert.jsp"; } //路径变量 @RequestMapping(value = "/{name}/{age}/first.do",method = {RequestMethod.POST,RequestMethod.GET}) public String doFirstcoller(@PathVariable() String name, @PathVariable() String age){ System.out.println(name); System.out.println(age); return "/WEB-INF/index.jsp"; } @RequestMapping(value = "/first.do",method = {RequestMethod.POST,RequestMethod.GET}) public String doFirst(String name, Model model){ System.out.println("name"+name); model.addAttribute("name"+name); return "index"; } }
请求中方式的定义:
method属性的取值为Requestmethod,是一个枚举常量
先创建一个UserInfo 实体类并写上属性,并进行封装:
域属性:一个类中变量的类型是另外一个自定义类型。
public class UserInfo { private String name; private Book book; private List<Book> books; public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { //集合属性 this.books = books; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
并且再创建Book的实体类,也写上一个属性,将其进行封装
public class Book { private String bookname; public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } }
之后创建ArgementController类
@Controller public class ArgementController { @RequestMapping("/login2") //限定请求方式 public String login2(UserInfo info){ System.out.println(info.getName()); return "/WEB-INF/delete.jsp"; } @RequestMapping("/AreArggement") public String login4(UserInfo info){ System.out.println(info.getName()+"\t"+info.getBook().getBookname()); return "/WEB-INF/delete.jsp"; }
//@RequestParam 校正参数名称 @RequestMapping("/rightArggument") public String logg3(@RequestParam("name") String uname2){ System.out.println(uname2); return "/WEB-INF/delete.jsp"; } @RequestMapping("/listArgument") public String listlogin5(UserInfo info){ System.out.println(info.getName()+"\t"+info.getBooks().get(0).getBookname()); return "/WEB-INF/delete.jsp"; } }
Web.xml
乱码解决forceEncoding
<!--javaWeb 三大件 servlet Filter listener --> <filter> <filter-name>CharactorEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharactorEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
来源:CSDN
作者:柠檬不萌耶
链接:https://blog.csdn.net/qq_36074150/article/details/77414943