Spring MVC 1.3.1 注解方式配置

虽然有两个方式配置,但是老师交的还有自己做项目都是用的注解的方式来配置Spring MVC 方式来配置。
1.首先第一步,先加载jar包,并且创建一个web项目:Spring 4.2.7.RELEASE:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar,将jar导入web-INF下的lib包下,并将该包导入 tomcat中。
2.在web.xml中配置spring-mvc.xml,
配置如下

<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*:/spring/spring-mvc.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>

3.在配置文件中配置springMvc.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:mvc="http://www.springframework.org/schema/mvc"
    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/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">

    <!-- 此处扫描@Controller 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
    <!-- <context:component-scan base-package="org.lanqiao.examples.*.api"> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" 
        /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" 
        /> </context:component-scan> -->

    <!-- 在指定包下扫描 此处扫描@Controller -->
    <context:component-scan
        base-package="org.lanqiao.**.api,org.lanqiao.**.controller,javacommon.web">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>


    <!-- 注解驱动的控制器配置 -->
    <mvc:annotation-driven>
        <!-- 解决org.springframework.beans.BeanInstantiationException: Failed to 
            instantiate [org.springframework.data.domain.Pageable]: Specified class is 
            an interface -->
        <mvc:argument-resolvers>
            <bean
                class="org.springframework.data.web.PageableHandlerMethodArgumentResolver" >
    <!-- <property name="sizeParameterName" value="pageSize"></property> -->
                </bean>
        </mvc:argument-resolvers>

        <mvc:message-converters register-defaults="true">
            <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="prettyPrint" value="true" /><!-- 让json的输出格式更好看 -->
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- JSR303 Validator定义 -->
    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />



    <!-- 静态资源,无需过控制器 -->
    <mvc:resources mapping="/static/**" location="/static/" />
    <mvc:resources mapping="/pages/**" location="/pages/" />
    <mvc:resources mapping="/page/**" location="/page/" />
    <mvc:resources mapping="/assets/**" location="/assets/" />
    <mvc:resources mapping="/example_library/**" location="/example_library/" />
    <!-- <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/css/**" 
        location="/css/" /> <mvc:resources mapping="/font/**" location="/font/" /> 
        <mvc:resources mapping="/images/**" location="/images/" /> -->
</beans>

4.一切准备工作做好之后,我们就直接编写程序代码,按道理是先从底层开始编写代码的(domain–>responsitory–>service–>api)
Domain 略过
Responsitory 略过
Service 略过
Api 即 Controller 层:

package org.lanqiao.rbac.api;

import java.util.ArrayList;
import java.util.Collections;

import javax.servlet.http.HttpServletRequest;

import org.lanqiao.rbac.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javacommon.utils.Ids;

@Controller("rbac_userController")
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private LoginService loginService;

    @ResponseBody
    @RequestMapping(value = "/login")
    public Map<String, String> login(String email , String password) {
        String token = loginService.login(email, password);
        return Collections.singletonMap("token", token);
    }
}

这里用了token来记录用户信息。
在这里我要叙述一下token的概念。
token相当于令牌,当用户执行相关操作的时候,系统应当检查当前用户是否登陆或者是否有该权限,否则操作失败或者返回登录页面进行登录操作。
而token存储方式类似Map,以键值对的方式存储起来,返回的形式以
Collections.singletonMap的方式返回,该返回方式的好处在于在方法调用返回一个只包含指定键 - 值映射的不可变映射。对于用户信息而言,token数据是不允许修改的。
而前台对于token的抓取的方法为window.localStorage来存储,而这个东西是h5新出的规范,每个浏览器规定有5MB大小的容量来存储用户信息,本地存储是一个window的属性,包括localStorage和sessionStorage,从名字应该可以很清楚的辨认二者的区别,前者是一直存在本地的,后者只是伴随着session,窗口一旦关闭就没了。
好,先打住,先讲一部分,后面token另开专区研究。

5.编写前台页面View
现在的前台页面都用静态页面来编写了,传递数据用Json传递,Json我也另开专区探讨,当初学的时候花了好久才了解Json到底是什么东西。
这里前台页面我也懒得写了,我就发前台发送一个Ajax请求请求到token数据,即用户信息,而前台只需要抓到它的键即可得到该用户的值,而该用户的值为该用户的id或者uuid。

<script>
            function submitForm(){
                $.post("/fullstack/api/user/login",$('#loginform').serialize(),function(result){
                    alert(result.token);
                    window.localStorage=result.token;
                  });
            }
        $(function(){
        })
    </script>

以上就是普通的RestFul风格的Spring MVC注解实现方式代码。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值