收藏列表
标题 | 标签 | 来源 | |
JdbcTemplate | spring jdbcTemplate使用 | ||
<?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" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <description>springApp</description> <!-- dataSource for MySQL --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/springapp" /> <property name="username" value="root" /> <property name="password" value="****" /> </bean> <bean id = "TransactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name = "dataSource" ref="dataSource"/> </bean> <!--1:配置一个JdbcTemplate实例,并将这个“共享的”,“安全的”实例注入到不同的DAO类中去--> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate"> <property name = "dataSource" ref="dataSource"/> </bean> <bean id = "actorJdbcTemplateDao" class = "com.logcd.bo.dao.impl.ActorJdbcTemplateDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <!--2:将共享的DataSource实例注入到DAO中,JdbcTemplate实例在DataSource的setter方法中被创建--> <bean id = "actorEventDao" class = "com.logcd.bo.dao.impl.ActorEventDaoImpl"> <property name = "dataSource" ref="dataSource"/> </bean> <!--利用了拦截器的原理。--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="operate*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="txProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Dao*</value><!--只是为了测试,一般为service--> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> </beans> |
|||
Struts2 Action处理多个请求 | struts2一个Action内包含多个请求处理方法的处理和多个action请求 | ||
Struts1提供了DispatchAction,从而允许一个Action内包含多个请求处理方法。Struts2也提供了类似的功能。处理方式主要有以下三种方式: 1.1. 动态方法调用: DMI:Dynamic Method Invocation 动态方法调用。 动态方法调用是指:表单元素的action不直接等于某个Action的名字,而是以如下形式来指定对应的动作名: <form method="post" action="userOpt!login.action"> 则用户的请求将提交到名为”userOpt”的Action实例,Action实例将调用名为”login”方法来处理请求。同时login方法的签名也是跟execute()一样,即为public String login() throws Exception。 注意:要使用动态方法调用,必须设置Struts2允许动态方法调用,通过设置struts.enable.DynamicMethodInvocation常量来完成,该常量属性的默认值是true。 1.1.1. 示例: 修改用户登录验证示例,多增加一个注册用户功能。 1. 修改Action类: Java代码 1.package org.qiujy.web.struts2.action; 2.import com.opensymphony.xwork2.ActionContext; 3.import com.opensymphony.xwork2.ActionSupport; 4. 5./** 6.*@authorqiujy 7.*@version1.0 8.*/ 9. 10.publicclass LoginAction extends ActionSupport{ 11.private String userName; 12.private String password; 13.private String msg; //结果信息属性 14./** 15. *@returnthemsg 16. */ 17. 18.public String getMsg() { 19. returnmsg; 20.} 21. 22./** 23. *@parammsgthemsgtoset 24. */ 25. 26.publicvoid setMsg(String msg) { 27. this.msg = msg; 28.} 29. 30./** 31. *@returntheuserName 32. */ 33. 34.public String getUserName() { 35. returnuserName; 36.} 37. 38./** 39. *@paramuserNametheuserNametoset 40. */ 41. 42.publicvoid setUserName(String userName) { 43. this.userName = userName; 44.} 45. 46./** 47. *@returnthepassword 48. */ 49. 50.public String getPassword() { 51. returnpassword; 52.} 53. 54./** 55. *@parampasswordthepasswordtoset 56. */ 57. 58.publicvoid setPassword(String password) { 59. this.password = password; 60.} 61. 62./** 63. *处理用户请求的login()方法 64. *@return结果导航字符串 65. *@throwsException 66. */ 67. 68.public String login() throws Exception{ 69. if("test".equals(this.userName) && "test".equals(this.password)){ 70. msg = "登录成功,欢迎" + this.userName; 71. //获取ActionContext实例,通过它来访问Servlet API 72. ActionContext context = ActionContext.getContext(); 73. //看session中是否已经存放了用户名,如果存放了:说明已经登录了; 74.//否则说明是第一次登录成功 75. if(null != context.getSession().get("uName")){ 76. msg = this.userName + ":你已经登录过了!!!"; 77. }else{ 78. context.getSession().put("uName", this.userName); 79. } 80. returnthis.SUCCESS; 81. }else{ 82. msg = "登录失败,用户名或密码错"; 83. returnthis.ERROR; 84. } 85.} 86. 87.public String regist() throws Exception{ 88. //将用户名,密码添加到数据库中 89. //... 90. msg = "注册成功。"; 91. returnthis.SUCCESS; 92.} 93. 94.} package org.qiujy.web.struts2.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** *@authorqiujy *@version1.0 */ publicclass LoginAction extends ActionSupport{ private String userName; private String password; private String msg; //结果信息属性 /** *@returnthemsg */ public String getMsg() { returnmsg; } /** *@parammsgthemsgtoset */ publicvoid setMsg(String msg) { this.msg = msg; } /** *@returntheuserName */ public String getUserName() { returnuserName; } /** *@paramuserNametheuserNametoset */ publicvoid setUserName(String userName) { this.userName = userName; } /** *@returnthepassword */ public String getPassword() { returnpassword; } /** *@parampasswordthepasswordtoset */ publicvoid setPassword(String password) { this.password = password; } /** *处理用户请求的login()方法 *@return结果导航字符串 *@throwsException */ public String login() throws Exception{ if("test".equals(this.userName) && "test".equals(this.password)){ msg = "登录成功,欢迎" + this.userName; //获取ActionContext实例,通过它来访问Servlet API ActionContext context = ActionContext.getContext(); //看session中是否已经存放了用户名,如果存放了:说明已经登录了; //否则说明是第一次登录成功 if(null != context.getSession().get("uName")){ msg = this.userName + ":你已经登录过了!!!"; }else{ context.getSession().put("uName", this.userName); } returnthis.SUCCESS; }else{ msg = "登录失败,用户名或密码错"; returnthis.ERROR; } } public String regist() throws Exception{ //将用户名,密码添加到数据库中 //... msg = "注册成功。"; returnthis.SUCCESS; } } 2. struts.xml文件:没有什么变化,跟以前一样配置 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <!-- 定义处理请求URL为login.action的Action --> <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction"> <!-- 定义处理结果字符串和资源之间的映射关系 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts> 3. 页面: index.jsp <%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用户登录页面</title> </head> <body> <h2>用户入口</h2> <hr> <form action="manage/userOpt!login.action" method="post"> <table border="1"> <tr> <td>用户名:</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 确定 "/> </td> </tr> </table> </form> </body> </html> regist.jsp <%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用户注册页面</title> </head> <body> <h2>用户注册</h2> <hr> <form action="manage/userOpt!regist.action" method="post"> <table border="1"> <tr> <td>用户名:</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 注册 "/> </td> </tr> </table> </form> </body> </html> 1.2. 为Action配置method属性: 将Action类中的每一个处理方法都定义成一个逻辑Action方法。 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> <action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction" method="regist"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts> 如上,把LoginAction中的login和regist方法都配置成逻辑Action。要调用login方法,则相应的把index.jsp中表单元素的action设置为"manage/userLogin.action";要调用regist方法,把regist.jsp中表单元素的action设置为"manage/userRegist.action"。 1.3. 使用通配符映射(wildcard mappings)方式: 在struts.xml文件中配置<action…>元素时,它的name、class、method属性都可支持通配符,这种通配符的方式是另一种形式的动态方法调用。 当我们使用通配符定义Action的name属性时,相当于用一个元素action定义了多个逻辑Action: <action name="user_*" class="org.qiujy.web.struts2.action.UserAction" method="{1}"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> 如上,<action name=”user_*”>定义一系列请求URL是user_*.action模式的逻辑Action。同时method属性值为一个表达式{1},表示它的值是name属性值中第一个*的值。例如:用户请求URL为user_login.action时,将调用到UserAction类的login方法;用户请求URL为user_regist.action时,将调用到UserAction类的regist方法。 ------------------------------------------ 多个action请求,strus_xml配置: [color=blue][align=center] <action name="Login" class="LoginAction"> <result name="View">/WEB-INF/jsp/system/login/login.jsp</result> <result name="Login" type="redirect-action">Login!welcome</result> <result name="Logout" type="redirect-action">Login!showLogin?error=${error}&username=${username}</result> <result name="Welcome">/WEB-INF/jsp/system/login/welcome.jsp</result> <interceptor-ref name="defaultStack"></interceptor-ref> </action>[/b][/color][/[/align]code] 原文:http://wallimn.iteye.com/blog/693158 |