SpringMVC 注解开发

先在一个包中创建一个类,然后再配置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>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值