`
zhuzhiguosnail
  • 浏览: 112667 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring经典注解实例一 10分钟弄懂

阅读更多
先来构建一个极为简单的web应用,从controller到dao。不考虑具体实现,只是先对整体架构有一个清晰的了解。



我们将用到如下jar包:

引用

aopalliance-1.0.jar
commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
spring-webmvc-2.5.6.jar


先看web.xml

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<web-app  
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4.    xmlns="http://java.sun.com/xml/ns/javaee"  
5.    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
6.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
7.    id="WebApp_ID"  
8.    version="2.5">  
9.    <display-name>spring</display-name>  
10.    <!-- 应用路径 -->  
11.    <context-param>  
12.        <param-name>webAppRootKey</param-name>  
13.        <param-value>spring.webapp.root</param-value>  
14.    </context-param>  
15.    <!-- Log4J 配置  -->  
16.    <context-param>  
17.        <param-name>log4jConfigLocation</param-name>  
18.        <param-value>classpath:log4j.xml</param-value>  
19.    </context-param>  
20.    <context-param>  
21.        <param-name>log4fRefreshInterval</param-name>  
22.        <param-value>60000</param-value>  
23.    </context-param>  
24.    <!--Spring上下文 配置  -->  
25.    <context-param>  
26.        <param-name>contextConfigLocation</param-name>  
27.        <param-value>/WEB-INF/applicationContext.xml</param-value>  
28.    </context-param>  
29.    <!-- 字符集 过滤器  -->  
30.    <filter>  
31.        <filter-name>CharacterEncodingFilter</filter-name>  
32.        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
33.        <init-param>  
34.            <param-name>encoding</param-name>  
35.            <param-value>UTF-8</param-value>  
36.        </init-param>  
37.        <init-param>  
38.            <param-name>forceEncoding</param-name>  
39.            <param-value>true</param-value>  
40.        </init-param>  
41.    </filter>  
42.    <filter-mapping>  
43.        <filter-name>CharacterEncodingFilter</filter-name>  
44.        <url-pattern>/*</url-pattern>  
45.    </filter-mapping>  
46.    <!-- Spring 监听器 -->  
47.    <listener>  
48.        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
49.    </listener>  
50.    <listener>  
51.        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
52.    </listener>  
53.    <!-- Spring 分发器 -->  
54.    <servlet>  
55.        <servlet-name>spring</servlet-name>  
56.        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
57.        <init-param>  
58.            <param-name>contextConfigLocation</param-name>  
59.            <param-value>/WEB-INF/servlet.xml</param-value>  
60.        </init-param>  
61.    </servlet>  
62.    <servlet-mapping>  
63.        <servlet-name>spring</servlet-name>  
64.        <url-pattern>*.do</url-pattern>  
65.    </servlet-mapping>  
66.    <welcome-file-list>  
67.        <welcome-file>index.html</welcome-file>  
68.        <welcome-file>index.htm</welcome-file>  
69.        <welcome-file>index.jsp</welcome-file>  
70.        <welcome-file>default.html</welcome-file>  
71.        <welcome-file>default.htm</welcome-file>  
72.        <welcome-file>default.jsp</welcome-file>  
73.    </welcome-file-list>  
74.</web-app>  
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<web-app 
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
4.    xmlns="http://java.sun.com/xml/ns/javaee" 
5.    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
6.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
7.    id="WebApp_ID" 
8.    version="2.5"> 
9.    <display-name>spring</display-name> 
10.    <!-- 应用路径 --> 
11.    <context-param> 
12.        <param-name>webAppRootKey</param-name> 
13.        <param-value>spring.webapp.root</param-value> 
14.    </context-param> 
15.    <!-- Log4J 配置  --> 
16.    <context-param> 
17.        <param-name>log4jConfigLocation</param-name> 
18.        <param-value>classpath:log4j.xml</param-value> 
19.    </context-param> 
20.    <context-param> 
21.        <param-name>log4fRefreshInterval</param-name> 
22.        <param-value>60000</param-value> 
23.    </context-param> 
24.    <!--Spring上下文 配置  --> 
25.    <context-param> 
26.        <param-name>contextConfigLocation</param-name> 
27.        <param-value>/WEB-INF/applicationContext.xml</param-value> 
28.    </context-param> 
29.    <!-- 字符集 过滤器  --> 
30.    <filter> 
31.        <filter-name>CharacterEncodingFilter</filter-name> 
32.        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
33.        <init-param> 
34.            <param-name>encoding</param-name> 
35.            <param-value>UTF-8</param-value> 
36.        </init-param> 
37.        <init-param> 
38.            <param-name>forceEncoding</param-name> 
39.            <param-value>true</param-value> 
40.        </init-param> 
41.    </filter> 
42.    <filter-mapping> 
43.        <filter-name>CharacterEncodingFilter</filter-name> 
44.        <url-pattern>/*</url-pattern> 
45.    </filter-mapping> 
46.    <!-- Spring 监听器 --> 
47.    <listener> 
48.        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
49.    </listener> 
50.    <listener> 
51.        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
52.    </listener> 
53.    <!-- Spring 分发器 --> 
54.    <servlet> 
55.        <servlet-name>spring</servlet-name> 
56.        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
57.        <init-param> 
58.            <param-name>contextConfigLocation</param-name> 
59.            <param-value>/WEB-INF/servlet.xml</param-value> 
60.        </init-param> 
61.    </servlet> 
62.    <servlet-mapping> 
63.        <servlet-name>spring</servlet-name> 
64.        <url-pattern>*.do</url-pattern> 
65.    </servlet-mapping> 
66.    <welcome-file-list> 
67.        <welcome-file>index.html</welcome-file> 
68.        <welcome-file>index.htm</welcome-file> 
69.        <welcome-file>index.jsp</welcome-file> 
70.        <welcome-file>default.html</welcome-file> 
71.        <welcome-file>default.htm</welcome-file> 
72.        <welcome-file>default.jsp</welcome-file> 
73.    </welcome-file-list> 
74.</web-app> 
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>spring</display-name>
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>spring.webapp.root</param-value>
</context-param>
<!-- Log4J 配置  -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4fRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!--Spring上下文 配置  -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 字符集 过滤器  -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Spring 分发器 -->
<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/servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

有不少人问我,这段代码是什么:

Xml代码 
1.<!-- 应用路径 --> 
2.<context-param>  
3.    <param-name>webAppRootKey</param-name>  
4.    <param-value>spring.webapp.root</param-value>  
5.</context-param>  
Xml代码
1.<!-- 应用路径 --> 
2.<context-param> 
3.    <param-name>webAppRootKey</param-name> 
4.    <param-value>spring.webapp.root</param-value> 
5.</context-param> 
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>spring.webapp.root</param-value>
</context-param>

这是当前应用的路径变量,也就是说你可以在其他代码中使用${spring.webapp.root}指代当前应用路径。我经常用它来设置log的输出目录。
为什么要设置参数log4jConfigLocation?

Xml代码 
1.<!-- Log4J 配置  --> 
2.    <context-param>  
3.        <param-name>log4jConfigLocation</param-name>  
4.        <param-value>classpath:log4j.xml</param-value>  
5.    </context-param>  
6.    <context-param>  
7.        <param-name>log4fRefreshInterval</param-name>  
8.        <param-value>60000</param-value>  
9.    </context-param>  
Xml代码
1.<!-- Log4J 配置  --> 
2.    <context-param> 
3.        <param-name>log4jConfigLocation</param-name> 
4.        <param-value>classpath:log4j.xml</param-value> 
5.    </context-param> 
6.    <context-param> 
7.        <param-name>log4fRefreshInterval</param-name> 
8.        <param-value>60000</param-value> 
9.    </context-param> 
<!-- Log4J 配置  -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4fRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>

这是一种基本配置,spring中很多代码使用了不同的日志接口,既有log4j也有commons-logging,这里只是强制转换为log4j!并且,log4j的配置文件只能放在classpath根路径。同时,需要通过commons-logging配置将日志控制权转交给log4j。同时commons-logging.properties必须放置在classpath根路径。
commons-logging内容:

Properties代码 
1.org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 
Properties代码
1.org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

最后,记得配置log4j的监听器:

Xml代码 
1.<listener> 
2.    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
3.</listener>  
Xml代码
1.<listener> 
2.    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
3.</listener> 
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

接下来,我们需要配置两套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于对应用层面做整体控制。按照分层思想,统领service层和dao层。servlet.xml则单纯控制controller层。

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans  
3.    xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
8.    <import  
9.        resource="service.xml" />  
10.    <import  
11.        resource="dao.xml" />  
12.</beans>  
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans 
3.    xmlns="http://www.springframework.org/schema/beans" 
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5.    xmlns:context="http://www.springframework.org/schema/context" 
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
8.    <import 
9.        resource="service.xml" /> 
10.    <import 
11.        resource="dao.xml" /> 
12.</beans> 
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<import
resource="service.xml" />
<import
resource="dao.xml" />
</beans>

applicationContext.xml什么都不干,它只管涉及到整体需要的配置,并且集中管理。
这里引入了两个配置文件service.xml和dao.xml分别用于业务、数据处理。
service.xml

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans  
3.    xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
8.    <context:component-scan  
9.        base-package="org.zlex.spring.service" />  
10.</beans>  
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans 
3.    xmlns="http://www.springframework.org/schema/beans" 
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5.    xmlns:context="http://www.springframework.org/schema/context" 
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
8.    <context:component-scan 
9.        base-package="org.zlex.spring.service" /> 
10.</beans> 
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="org.zlex.spring.service" />
</beans>

注意,这里通过<context:component-scan />标签指定了业务层的基础包路径——“org.zlex.spring.service”。也就是说,业务层相关实现均在这一层。这是有必要的分层之一。
dao.xml

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans  
3.    xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5.    xmlns:aop="http://www.springframework.org/schema/aop"  
6.    xmlns:context="http://www.springframework.org/schema/context"  
7.    xmlns:tx="http://www.springframework.org/schema/tx"  
8.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
9.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
10.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
11.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">  
12.    <context:component-scan  
13.        base-package="org.zlex.spring.dao" />  
14.</beans>  
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans 
3.    xmlns="http://www.springframework.org/schema/beans" 
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5.    xmlns:aop="http://www.springframework.org/schema/aop" 
6.    xmlns:context="http://www.springframework.org/schema/context" 
7.    xmlns:tx="http://www.springframework.org/schema/tx" 
8.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
9.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
10.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
11.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
12.    <context:component-scan 
13.        base-package="org.zlex.spring.dao" /> 
14.</beans> 
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="org.zlex.spring.dao" />
</beans>

dao层如法炮制,包路径是"org.zlex.spring.dao"。从这个角度看,注解还是很方便的!
最后,我们看看servlet.xml

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans  
3.    xmlns="http://www.springframework.org/schema/beans"  
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5.    xmlns:context="http://www.springframework.org/schema/context"  
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
8.    <context:component-scan  
9.        base-package="org.zlex.spring.controller" />  
10.    <bean  
11.        id="urlMapping"  
12.        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
13.    <bean  
14.        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
15.</beans>  
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans 
3.    xmlns="http://www.springframework.org/schema/beans" 
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5.    xmlns:context="http://www.springframework.org/schema/context" 
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
8.    <context:component-scan 
9.        base-package="org.zlex.spring.controller" /> 
10.    <bean 
11.        id="urlMapping" 
12.        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
13.    <bean 
14.        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
15.</beans> 
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="org.zlex.spring.controller" />
<bean
id="urlMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

包路径配置就不细说了,都是一个概念。最重要的时候后面两个配置,这将使得注解生效!
“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”是默认实现,可以不写,Spring容器默认会默认使用该类。
“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”直接关系到多动作控制器配置是否可用!
简单看一下代码结构,如图:

Account类是来存储账户信息,属于域对象,极为简单,代码如下所示:
Account.java

Java代码 
1./** 
2. * 2010-1-23 
3. */ 
4.package org.zlex.spring.domain;   
5. 
6.import java.io.Serializable;   
7. 
8./** 
9. *  
10. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> 
11. * @version 1.0 
12. * @since 1.0 
13. */ 
14.public class Account implements Serializable {   
15. 
16.    /** 
17.     *  
18.     */ 
19.    private static final long serialVersionUID = -533698031946372178L;   
20. 
21.    private String username;   
22.    private String password;   
23. 
24.    /** 
25.     * @param username 
26.     * @param password 
27.     */ 
28.    public Account(String username, String password) {   
29.        this.username = username;   
30.        this.password = password;   
31.    }  
32. 
33.    /** 
34.     * @return the username 
35.     */ 
36.    public String getUsername() {   
37.        return username;   
38.    }  
39. 
40.    /** 
41.     * @param username the username to set 
42.     */ 
43.    public void setUsername(String username) {   
44.        this.username = username;   
45.    }  
46. 
47.    /** 
48.     * @return the password 
49.     */ 
50.    public String getPassword() {   
51.        return password;   
52.    }  
53. 
54.    /** 
55.     * @param password the password to set 
56.     */ 
57.    public void setPassword(String password) {   
58.        this.password = password;   
59.    }  
60.      
61.      
62.} 
Java代码
1./** 
2. * 2010-1-23 
3. */ 
4.package org.zlex.spring.domain;  
5. 
6.import java.io.Serializable;  
7. 
8./** 
9. *  
10. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> 
11. * @version 1.0 
12. * @since 1.0 
13. */ 
14.public class Account implements Serializable {  
15. 
16.    /** 
17.     *  
18.     */ 
19.    private static final long serialVersionUID = -533698031946372178L;  
20. 
21.    private String username;  
22.    private String password;  
23. 
24.    /** 
25.     * @param username 
26.     * @param password 
27.     */ 
28.    public Account(String username, String password) {  
29.        this.username = username;  
30.        this.password = password;  
31.    }  
32. 
33.    /** 
34.     * @return the username 
35.     */ 
36.    public String getUsername() {  
37.        return username;  
38.    }  
39. 
40.    /** 
41.     * @param username the username to set 
42.     */ 
43.    public void setUsername(String username) {  
44.        this.username = username;  
45.    }  
46. 
47.    /** 
48.     * @return the password 
49.     */ 
50.    public String getPassword() {  
51.        return password;  
52.    }  
53. 
54.    /** 
55.     * @param password the password to set 
56.     */ 
57.    public void setPassword(String password) {  
58.        this.password = password;  
59.    }  
60.      
61.      
62.} 
/**
* 2010-1-23
*/
package org.zlex.spring.domain;

import java.io.Serializable;

/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
public class Account implements Serializable {

/**
*
*/
private static final long serialVersionUID = -533698031946372178L;

private String username;
private String password;

/**
* @param username
* @param password
*/
public Account(String username, String password) {
this.username = username;
this.password = password;
}

/**
* @return the username
*/
public String getUsername() {
return username;
}

/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}


}

通常,在构建域对象时,需要考虑该对象可能需要进行网络传输,本地缓存,因此建议实现序列化接口Serializable
我们再来看看控制器,这就稍微复杂了一点代码如下所示:
AccountController .java

Java代码 
1./** 
2. * 2010-1-23 
3. */ 
4.package org.zlex.spring.controller;   
5. 
6.import javax.servlet.http.HttpServletRequest;   
7.import javax.servlet.http.HttpServletResponse;   
8. 
9.import org.springframework.beans.factory.annotation.Autowired;   
10.import org.springframework.stereotype.Controller;   
11.import org.springframework.web.bind.ServletRequestUtils;   
12.import org.springframework.web.bind.annotation.RequestMapping;   
13.import org.springframework.web.bind.annotation.RequestMethod;   
14.import org.zlex.spring.service.AccountService;   
15. 
16./** 
17. *  
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> 
19. * @version 1.0 
20. * @since 1.0 
21. */ 
22.@Controller  
23.@RequestMapping("/account.do")   
24.public class AccountController {   
25. 
26.    @Autowired  
27.    private AccountService accountService;   
28. 
29.    @RequestMapping(method = RequestMethod.GET)   
30.    public void hello(HttpServletRequest request, HttpServletResponse response)   
31.            throws Exception {   
32. 
33.        String username = ServletRequestUtils.getRequiredStringParameter(  
34.                request, "username");   
35.        String password = ServletRequestUtils.getRequiredStringParameter(  
36.                request, "password");   
37.        System.out.println(accountService.verify(username, password));  
38.    }  
39.} 
Java代码
1./** 
2. * 2010-1-23 
3. */ 
4.package org.zlex.spring.controller;  
5. 
6.import javax.servlet.http.HttpServletRequest;  
7.import javax.servlet.http.HttpServletResponse;  
8. 
9.import org.springframework.beans.factory.annotation.Autowired;  
10.import org.springframework.stereotype.Controller;  
11.import org.springframework.web.bind.ServletRequestUtils;  
12.import org.springframework.web.bind.annotation.RequestMapping;  
13.import org.springframework.web.bind.annotation.RequestMethod;  
14.import org.zlex.spring.service.AccountService;  
15. 
16./** 
17. *  
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> 
19. * @version 1.0 
20. * @since 1.0 
21. */ 
22.@Controller 
23.@RequestMapping("/account.do")  
24.public class AccountController {  
25. 
26.    @Autowired 
27.    private AccountService accountService;  
28. 
29.    @RequestMapping(method = RequestMethod.GET)  
30.    public void hello(HttpServletRequest request, HttpServletResponse response)  
31.            throws Exception {  
32. 
33.        String username = ServletRequestUtils.getRequiredStringParameter(  
34.                request, "username");  
35.        String password = ServletRequestUtils.getRequiredStringParameter(  
36.                request, "password");  
37.        System.out.println(accountService.verify(username, password));  
38.    }  
39.} 
/**
* 2010-1-23
*/
package org.zlex.spring.controller;

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.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;

/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {

@Autowired
private AccountService accountService;

@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {

String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
}

分段详述:

Java代码 
1.@Controller 
2.@RequestMapping("/account.do")  
Java代码
1.@Controller 
2.@RequestMapping("/account.do") 
@Controller
@RequestMapping("/account.do")

这两行注解,@Controller是告诉Spring容器,这是一个控制器类,@RequestMapping("/account.do")是来定义该控制器对应的请求路径(/account.do)

Java代码 
分享到:
评论

相关推荐

    画图带你彻底弄懂三级缓存和循环依赖的问题.doc

    在 Bean 初始化阶段,Spring 进行注解处理、动态代理等操作,确保 Bean 完成全部初始化后放入一级缓存 singletonObjects。\n\n需要注意的是,这些缓存机制仅对单例 Bean 生效,因为多例(Prototype)Bean 的生命周期...

    springmvc+ibatis

    springmvc+ibatis实例,只要修改一下数据配置文件,并创建相应的数据库,表,表只有一个tab_User,很好用的,实例是我花了好久才弄出来,各种包齐全,包绝对不多,也不少,就是刚刚好的感觉,懂吗?就是不会出现多余...

    没弄懂这些Java基础,简历上千万别写熟悉:异常+反射+注解+泛型

    - `throw`: 用于抛出一个具体的异常实例。 - `throws`: 用于声明一个方法可能会抛出的异常类型。 - **捕获异常**:使用 `try-catch` 块来捕获并处理异常。这通常是最常见的处理异常的方式,可以针对性地处理特定...

    全球变风量(VAV)系统市场研究:年复合增长率(CAGR)为 5.8%

    在全球建筑行业不断追求节能与智能化发展的浪潮中,变风量(VAV)系统市场正展现出蓬勃的发展潜力。根据 QYResearch 报告出版商的深入调研统计,预计到 2031 年,全球变风量(VAV)系统市场销售额将飙升至 1241.3 亿元,在 2025 年至 2031 年期间,年复合增长率(CAGR)为 5.8%。这一令人瞩目的数据,不仅彰显了 VAV 系统在当今建筑领域的重要地位,更预示着其未来广阔的市场前景。​ 变风量系统的起源可追溯到 20 世纪 60 年代的美国。它犹如建筑空调系统中的 “智能管家”,能够敏锐地感知室内负荷或室内所需参数的变化,通过维持恒定的送风温度,自动、精准地调节空调系统的送风量,从而确保室内各项参数始终满足空调系统的严格要求。从系统构成来看,变风量系统主要由四个基本部分协同运作。变风量末端设备,包括 VAV 箱和室温控制器,如同系统的 “神经末梢”,负责接收室内环境变化的信号并做出初步响应;空气处理及输送设备则承担着对空气进行净化、加热、冷却等处理以及高效输送的重任;风管系统,涵盖新风、排风、送风、回风等管道,构建起了空气流通的 “高速公路”;而自动控制系统宛

    《基于YOLOv8的跆拳道训练系统》(包含源码、完整数据集、可视化界面、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计.zip

    资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。

    探究ChatGPT情感化交互对其用户情绪健康的多方法研究

    内容概要:本文探讨了ChatGPT这种高级语音模式的人工智能聊天机器人与用户的互动对其情绪健康的影响。研究采用了两种互补的方法:大规模平台数据分析和随机对照试验(RCT)。平台数据部分通过对超过400万次对话进行隐私保护的大规模自动化分析以及对4000多名用户的调查,揭示了高频率使用者表现出更多的情感依赖和较低的社会交往意愿。RCT部分则通过近1000名参与者为期28天的研究,发现语音模型相较于文本模型能带来更好的情绪健康效果,但长时间使用可能导致负面后果。此外,初始情绪状态较差的用户在使用更具吸引力的语音模型时,情绪有所改善。 适合人群:对人机交互、情感计算和社会心理学感兴趣的科研人员和技术开发者。 使用场景及目标:本研究旨在为AI聊天机器人的设计提供指导,确保它们不仅能满足任务需求,还能促进用户的心理健康。同时,也为政策制定者提供了关于AI伦理使用的思考。 其他说明:研究强调了长期使用AI聊天机器人可能带来的复杂心理效应,特别是对于那些已经感到孤独或社交孤立的人来说,过度依赖可能会加剧这些问题。未来的研究应该更加关注这些极端情况下的用户体验。

    Java反射性能优化:深入探讨setAccessible与MethodHandle的技术差异及应用场景

    Java 反射(Reflection)是一种强大的机制,允许程序在运行时检查和操作类的成员变量和方法。然而,传统的 `setAccessible(true)` 方式虽然便捷,但存在安全性问题,并且性能相对较低。在 Java 7 引入 `MethodHandle` 后,我们可以通过 `MethodHandles.Lookup.findVirtual()` 提供更优雅、高效的方式来访问对象属性。本文将对比这两种反射方式,并分析它们的优缺点。

    loongdomShop.tar.gz

    loongdomShop.tar.gz

    人工智能与人类行为对聊天机器人社会心理效应的纵向随机对照研究

    内容概要:本文探讨了不同交互模式(文本、中性语音、吸引人语音)和对话类型(开放式、非个人化、个人化)对聊天机器人使用者的心理社会效果(如孤独感、社交互动、情感依赖、不当使用)的影响。研究表明,在初期阶段,语音型聊天机器人比文本型更能缓解孤独感并减少情感依赖,但随着每日使用时间增加,这种优势逐渐消失,尤其是对于中性语音聊天机器人。此外,个人话题对话略微增加了孤独感,而非个人话题则导致更高的情感依赖。总体而言,高频率使用聊天机器人的用户表现出更多的孤独感、情感依赖和不当使用,同时减少了真实人际交往。研究还发现,某些个体特征(如依恋倾向、情绪回避)使用户更容易受到负面影响。 适合人群:心理学家、社会学家、人工智能研究人员以及关注心理健康和人机交互的专业人士。 使用场景及目标:①帮助理解不同类型聊天机器人对用户心理健康的潜在影响;②为设计更健康的人工智能系统提供指导;③制定政策和规范,确保聊天机器人的安全和有效使用。 其他说明:研究强调了进一步探索聊天机器人管理情感内容而不引发依赖或替代人际关系的重要性,呼吁更多跨学科的研究来评估长期影响。

    MP4575GF-Z 产品规格书

    MP4575GF-Z MP4575 TSSOP-20 降压型可调DC-DC电源芯片

    界面设计_SwiftUI_习惯养成_项目管理_1742850611.zip

    界面设计_SwiftUI_习惯养成_项目管理_1742850611.zip

    免安装版的logic软件包 支持波形实时查看 内含驱动文件

    免安装版的logic软件包。支持波形实时查看。内含驱动文件。

    基于Springboot+Mysql的学生毕业离校系统(含LW+PPT+源码+系统演示视频+安装说明).zip

    1. **系统名称**:学生毕业离校系统 2. **技术栈**:Java技术、MySQL数据库、Spring Boot框架、B/S架构、Tomcat服务器、Eclipse开发环境 3. **系统功能**: - **管理员功能**:首页、个人中心、学生管理、教师管理、离校信息管理、费用结算管理、论文审核管理、管理员管理、留言板管理、系统管理。 - **学生功能**:首页、个人中心、费用结算管理、论文审核管理、我的收藏管理。 - **教师功能**:首页、个人中心、学生管理、离校信息管理、费用结算管理、论文审核管理。

    WebSocket测试Demo程序

    配套文章:https://blog.csdn.net/gust2013/article/details/139608432

    蓝凌OA系统V15.0管理员手册

    蓝凌OA系统V15.0管理员手册

    《基于YOLOv8的生物样本识别系统》(包含源码、完整数据集、可视化界面、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计.zip

    资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。

    mips-gcc520-glibc222编译工具链.zip

    mips-gcc520-glibc222编译工具链.zip

    社交网络_React_Native_开发教程_学习资源_1742847416.zip

    app开发

    Swift编程语言的基础特性与应用开发入门教程

    内容概要:本文档详细介绍了Swift编程语言的基础知识,涵盖语言特点、基础语法、集合类型、控制流、函数定义、面向对象编程、可选类型、错误处理、协议与扩展以及内存管理等方面的内容。此外还简要提及了Swift与UIKit/SwiftUI的关系,并提供了进一步学习的资源推荐。通过这份文档,读者可以全面了解Swift的基本概念及其在iOS/macOS/watchOS/tvOS平台的应用开发中的使用方法。 适合人群:初学者或者希望从其他编程语言转向Swift的开发者。 使用场景及目标:帮助读者快速上手Swift编程,掌握其基本语法和特性,能够独立完成简单的程序编写任务,为进一步学习高级主题如并发编程、图形界面设计打下坚实的基础。 阅读建议:由于Swift是一门现代化的语言,拥有许多独特的特性和最佳实践方式,在学习过程中应当多加练习并尝试理解背后的原理。同时利用提供的官方文档和其他辅助材料加深印象。

    《基于YOLOv8的泰拳训练辅助系统》(包含源码、完整数据集、可视化界面、部署教程)简单部署即可运行。功能完善、操作简单,适合毕设或课程设计.zip

    资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。

Global site tag (gtag.js) - Google Analytics