通知
此博客运行在jpress系统上,如果你喜欢此博客模板,请加QQ群:1061691290(whimurmur模板/jpress插件),免费下载使用

springMVC学习记录2-使用注解配置

3464人浏览 / 0人评论 | 作者:whisper  | 分类: springmvc  | 标签: springmvc  | 

作者:whisper

链接:http://proprogrammar.com:443/article/372

声明:请尊重原作者的劳动,如需转载请注明出处


  前面说了一下使用xml配置springmvc,下面再说说注解配置。项目如下:

  

  业务很简单,主页和输入用户名和密码进行登陆的页面。

  看一下springmvc的配置文件:

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="com.sxt"/>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
         p:suffix=".jsp"/>
</beans>

  设置了一下处理器适配器和视图解析器,在高版本的springmvc中不需要设置处理器适配器和处理器映射器,只需要加一句

<mvc:annotation-driven />

  关于这句话做了什么,可以参看下面的文章:

  spring mvc拦截器和<mvc:annotation-driven />的详解

  这就是一个最简单的springmvc配置文件的内容了,下面来看一下controller的内容:

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sxt.po.User;
import com.sxt.service.UserService;

@Component
@RequestMapping("/user.do")
public class UserController  {

    @Resource
    private UserService userService;
    
    
    @RequestMapping(params="method=reg")
    public String reg(String uname){
        System.out.println("UserController.reg()");
        System.out.println(uname);
        userService.add(uname);
        return "index";
    }
    
    @RequestMapping(params="method=reg2")
    public String reg2(User user){
        System.out.println("UserController.reg2()");
        System.out.println(user.getUname());
        return "index";
    }
    
    

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    
}

  相关spring的配置就不贴出来了,使用了注解,@Component(类似的还有@Controller,@Service, @Repository),@Resource(类似的还有@Autowired),@RequestMapping,@RequestParam,@PathVariable等等,具体可以参看下面的文章:

  springmvc常用注解标签详解

   还要补充一个东西就是Model和ModelAndView,可以看下面的文章:

  SPRING框架中ModelAndView、Model、ModelMap区别【转】

  另外在配置文件中常用的设置还有处理静态资源的一个:

<mvc:resources>

  关于它的使用可以参看下面的文章:

  spring-mvc里的 <mvc:resources> 及静态资源访问

  它的写法如:

<mvc:resources location="/img/" mapping="/img/**"/>   
<mvc:resources location="/js/" mapping="/js/**"/>    
<mvc:resources location="/css/" mapping="/css/**"/>  

  说到这,再补充一个关于路径中*和**的区别的知识:

  配置扫描路径详细说明 * ** ?的含义

  大致就是这么多内容,现在可以自己使用注解配置springmvc项目了,完整配置使用的例子网上很多,springmvc本身也很简单,很容易就可以学会,大家可以自己找些资料来学习。下一篇文章开始学习springmvc高级一点的东西,拦截器和文件上传。


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!