spring mvc 注解 学习笔记(一)

以前接触过spring,但是没有接触spring mvc 以及注解的应用,特习之,记之:

注解了解

@Component 是通用标注,

@Controller 标注web控制器,

@Service 标注Servicec 层的服务,

@Respository 标注 DAO层的数据访问

其实,这四个注解的效果都是一样的,Spring都会把它们当做需要注入的Bean加载在上下文中;

但是在项目中,却建议严格按照除Componen的其余三个注解的含义使用在项目中。这对分层结构的web架构很有好处!!

@Autowired  标注Autowired  它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作,

@RequestMapping  可以声明到类或方法上  具体用法如下示例,当然还有许多其他的注解标记:

配置:myeclipse 8.5  tomcat jdk1.6

第一步、导入jar包 core  web aop ,右键工程 -myeclipse -add spring   选择即可,以下是工程目录

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQkAAAF4CAIAAADIdJtXAAAgAElEQVR4nO2dbWxU153G/SXfkai0zYeVUhSxICQQqIrESEtZ2o0J2W1ZJFpItgrqptNmiZqGplvS1I1xY+IQ2ExZmxISDFHi1CE4S9bxCGgsIBRDNig4oXG8maVgIMUMNvFSEsa89OyHO3PnvPzPvXdm7uuZ56fzYXxfzjl3fJ57zrlznvtvYDWz9P4H+l77mZ1qzxCAONBQexZL739AkgcUAgygem180PP4Bz2PW8JYev8DjDFoA5hEldoY3P249YHXho30Zw1k0w0W6axPOQLgkcq00bHumY51z3Ssa7t+sfP6xc4vTm+xtdGxrs1OqlqqIpdJFSWRTUMdIGwIbRw4cKCtrY08+pknNrX++hj7v3f4tPT+B9Qt/nUdjDHGsumGVCbnZ44AuCBro7+/f9asWdOnT9edsObR/2CXevlUVIL4JykProVzPUFpay6TatCMoCANED6CNixhrFixwkEb//KD59mnr0hJekil00Yukyo2+2w6lUpZzb3Y7sv7pBMw2wDRUNaGLYyVK1c6aIMxln93x0jX8vy7Oxhjqx7sXPXg9lUPblflserB7fKZJQFk06lMLpsuasLWSEODpnvIpnV7AAiKojZ4YbhqY6RrObt5cqRrOWPszZFbduK18ebIrX/655eVUy0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HCX9l9+9sYVhbqgoT7j+OP4tDG0klAdrwO0y4rxSHUA6NH9pIKgnQBsKEg0j4f4lp6KSAm571AAAAAElFTkSuQmCC" alt="" />

第二步、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置前端控制器 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-*.xml</param-value><!-- spring配置文件 。这里可以拓展一些其他配置文件,如数据库等 。如果该参数为空,spring会默认创建一个以servlet-name加后缀-servlet.xml的文件读取 -->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.htm</url-pattern><!--  *.htm拦截请求后缀,可*定义,如*.do  *.action  -->
    </servlet-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第三步、配置spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 扫描Bean的范围 使注解生效 -->
<context:component-scan base-package="com.cd" /> <!-- 配置视图 视图的前缀 后缀 在这里配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

第四步、SprController.java ,

package com.cd;

import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @Controller//控制层注解标记,这个pojo就成为一个控制器
@RequestMapping("reg")//访问路径
public class RegController { @Autowired //Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
private RegDao regdao; @RequestMapping("/reguser")//访问路径,调用这个方法路径:工程名/reg/reguser.htm,这里还有一些其他参数 @PathVariable、params、method 。。。
public String reguser(HttpServletRequest req,HttpServletResponse resp) { Map<String,String> regmap = new HashMap<String,String>();
regmap.put("username", req.getParameter("username"));
regmap.put("password",req.getParameter("password"));
regmap.put("remark",req.getParameter("remark"));
regdao.insertReg((Map<String, String>) regmap);
return "index"; //返回视图名称 前缀及后缀在配置文件中,这里表示/WEB-INF/index.jsp
} }

RegDao.java  这里只是证明调用到了该类的方法

package com.cd;

import java.util.Map;

import org.springframework.stereotype.Repository;

@Repository  //跟controller作用差不多
public class RegDao{
public boolean insertReg(Map<String,String> map) { System.out.println("dao============");
return false;
}
}

到此完成,运行代码访问 :http://localhost:8080/ws3/reg/reguser.htm,控制器会调用到RegDao 输出dao==========并且跳转到:http://localhost:8080/ws3/WEB-INF/index.jsp

一个基于注解的spring就完成了,如果要灵活应用,还得继续学习!

上一篇:php对文本文件进行分页功能简单实现


下一篇:Android中的Zip解压缩