您的位置:首页 > 编程语言 > Java开发

学习笔记3:Struts2+Spring JDBC+Spring

2015-11-22 10:04 417 查看
Spring有许多功能,这里我所使用到的只是用于管理容器和Spring监听器的作用。

Spring用于管理容器时,需要添加applicationContext.xml文档系列

applicationContext.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: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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="UserDao" class="org.kingtoon.dao.impl.UserDaoImpl" />
<bean id="LoginAction" class="org.kingtoon.action.LoginAction"/>
<bean id="RegisterAction" class="org.kingtoon.action.RegisterAction"/>

</beans>


其中的beans用于类,接口名称的定义,之后struts.xml文档系列中,可直接用名称指代类

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.multipart.maxSize" value="10000000000" />
<constant name="struts.multipart.saveDir" value="/tmp" />
<!-- 登陆 -->
<include file="struts-example.xml"/>
<include file="struts-example2.xml"/>
</struts>


struts-examle.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="example" namespace="/example" extends="struts-default">
<action name="register" class="RegisterAction" method="Regist">
<result name="success">/Congratulation.jsp</result>
<result name="findit">/Login.jsp</result>
<!-- <result name="input">/Register.jsp</result> -->
</action>
</package>

</struts>


struts-example2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="example2" namespace="/example2" extends="struts-default">
<action name="login" class="org.kingtoon.action.LoginAction" method="Login">
<result name="success">/Welcome.jsp</result>
<result name="fail">/Register.jsp</result>
<result name="input">/Login.jsp</result>
</action>
</package>
</struts>


另外,Spring监听器在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"> 
<!-- 配置spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>org.kingtoon.filter.LoginFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--struts的配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>


这些都是新加入的配置

其他的类都与笔记2中的一样,不需要改变
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: