用注解实现springMVC

来源:互联网 发布:java返回值是什么意思 编辑:程序博客网 时间:2024/05/29 10:12

1.创建Webmaven项目

2.导入所需要的jar包
  <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.4.1</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.6</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.3.0</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.17</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7.12</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.7.12</version>        </dependency>        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.1.0</version>            <scope>provided</scope>        </dependency>        <!-- https://mvnrepository.com/artifact/javax/javaee-api -->        <dependency>            <groupId>javax</groupId>            <artifactId>javaee-api</artifactId>            <version>7.0</version>            <scope>provided</scope>        </dependency>        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>
3.创建两个实体类和两个jsp

Book实体类
public class Book {    private String bookname;    public String getBookname() {        return bookname;    }    public void setBookname(String bookname) {        this.bookname = bookname;    }}
UserInfo实体类
   private String uname;    private  Book book;    private List<Book> bookList;

login.jsp
<body><form  method="post" action="${pageContext.request.contextPath}/five">    用户名<input name="uname"/>    图书名(域属性)<input name="book.bookname"/>    图书名(域属性集合)<input name="bookList[0].bookname"/>           <input type="submit"></form></body>

跳转页面

 4.配置springmvc.xml(用于扫描注解)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">    <!--注解扫描-->  <context:component-scan base-package="cn.happy.Annotation"></context:component-scan></beans>


5.在web.xml配置前端控制器

<servlet>    <servlet-name>springmvc</servlet-name>    <!--引入前端控制器-->    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <!--映射springMVC.xml-->      <param-value>classpath:springMVC_Annotation.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>

6.创建控制器

//控制器@Controller//访问路径@RequestMapping("/User")public class MyControler {    @RequestMapping("/first")    public String doFirst(){        return"/WEB-INF/index.jsp";    }    @RequestMapping("/second")    //测试传输单个值    public String doSecond(String uname){        System.out.println(uname);        return "/WEB-INF/index.jsp";    }    @RequestMapping("/third")    //传输打单个对象    public String doThird(UserInfo userInfo){        System.out.println(userInfo.getUname());        return "/WEB-INF/index.jsp";    }    //传输域属性    @RequestMapping("/four")    public String doFour(UserInfo userInfo){        System.out.println(userInfo.getUname()+"\t"+userInfo.getBook().getBookname());        return "/WEB-INF/index.jsp";    }    //传输域属性集合    @RequestMapping("/five")    public String doFive(UserInfo userInfo){        System.out.println(userInfo.getUname()+userInfo.getBookList().get(0).getBookname());        return "/WEB-INF/index.jsp";    }    //路径变量(获取路径中的值)    @RequestMapping("/{uname}/{upwd}/six")    public String doSix(@PathVariable("uname") String name,@PathVariable("upwd") String pwd){        System.out.println(name+pwd);        return  "/WEB-INF/index.jsp";    }}




原创粉丝点击