Spring MVC 注解

来源:互联网 发布:mac上前端开发软件 编辑:程序博客网 时间:2024/06/06 01:20

SpringMVC注解式开发

首先我们的架构:




然后我们开始了编码,首先

我们看一下web.xml

我们的前端控制器什么的 我就不 多说废话了请看我的上一篇博客,SpringMVC xml配置 的版本。

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Archetype Created Web Application</display-name>  <!--字符编码过滤器-->  <filter>    <filter-name>charaterEncoding</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>charaterEncoding</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <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:springmvc04auto.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>  <servlet-mapping>    <servlet-name>default</servlet-name>    <url-pattern>*.jpg</url-pattern>  </servlet-mapping></web-app>

然后我们看一下spring.xml  因为我们是注解的开发,所有不需要弄一下别的 配置 ,只需要一个包扫描器。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"               xmlns:aop="http://www.springframework.org/schema/aop"               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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="cn.happy.springmvc04"></context:component-scan></beans>

然后我们看一下controller层,这里因为需要在类名的上方弄一个@Controller的注解

然后我们可以写方法了,写一个普通的方法,返回的路径是我们的页面,因为我的页面时在WEB-INF下的  所以我的路径是这样的,在然后我们需要在方法上写一个@RequestMapping("/...")的注解,然后我们在页面通过这个路径来访问我们的页面。

package cn.happy.springmvc04;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by linlin on 2017/8/18. */@Controllerpublic class FirstController {    @RequestMapping("/first")    public String doFirst(){return "/WEB-INF/doFirst.jsp";    }    @RequestMapping("/second")    public String doSecond(){        return "/WEB-INF/index.jsp";    }}


页面显示:

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><%    String path = request.getContextPath();    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><html><head>    <title>doFirst</title></head><body><h1>doFirst</h1><img src="<%=path%>/img/5.jpg"/></body></html>



上面是一个简单的注解示例

下面我们在深入了解一下 关于注解。。。

web.xml  和spring.xml这个我们每有变化都是这个  所有我就不写了。

我们只看一下Controller和页面

首先我们看一下页面

这个页面是我根据索引获取的 name值

首先我们的form表单提交的地址 是我们在Controller层的注解的value值

<%--  Created by IntelliJ IDEA.  User: linlin  Date: 2017/8/18  Time: 11:10  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><%    String path = request.getContextPath();    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><html><head>    <title>Login</title></head><body><h1>Login</h1><form method="post" action="<%=path%>/fourth">   用户名: <input name="uname">    图书名称:<input name="list[0].bookname"/>    图书名称:<input name="list[1].bookname"/>    <input type="submit"></form></body></html>

看一下Controller页面、

前俩个分别的是我们根据 直接的 name 值 ,获取的Userinfo对象获取到的页面的值,我们会在控制台写出来。

第三个方法是域属性 我们在Userinfo里面有一个对象 是Book对象。对应的是我们Book实体类的


第三个集合的那个是我们在页面上写的那个。

最后一个 我们不需要在页面  只要在路径上面写就可以 在页面打印。



package cn.happy.springmvc04;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by linlin on 2017/8/18. */@Controllerpublic class FirstController2 {    @RequestMapping("/singlefirst")    public String doFirst(String uname){        System.out.println(uname);return "/WEB-INF/doFirst.jsp";    }    @RequestMapping("/singsecond")    public String doSecond(UserInfo info){        System.out.println(info.getUname());        return "/WEB-INF/index.jsp";    }    @RequestMapping("/singBook")    public String doBook(UserInfo info){        System.out.println(info.getUname()+"\t"+info.getBook().getBookname());        return "/WEB-INF/index.jsp";    }/*集合*/    @RequestMapping(value= "/fourth")    public String doFourth(Model model,UserInfo info){        System.out.println(info.getUname()+"\t"+info.getList().get(0).getBookname());        return "/WEB-INF/index.jsp";    }    @RequestMapping(value= "/{rname}/{age}/hehe")    public String doFourth11(@PathVariable("rname") String name,@PathVariable("age") int age){        System.out.println(name);        System.out.println(age);        return "/WEB-INF/index.jsp";    }}



原创粉丝点击