新手研究spring mvc框架
大家好,刚把javaee学完,接着趁热打铁学框架,这是我搭建的一个spring框架,搭的不好的地方请大家指点,,我用的spring2.5的jar,听说这个版本比较稳定,下面介绍我的框架是怎么搭建的
1.新建一个web工程,工程名称是spring
2.导入相应的springjar文件
3.在src目录下建立相应的包
(1)
com.spring.dao 新建一个MemberDao接口
package com.spring.dao; import java.util.List; import com.spring.model.MembersModel; /** * @author Administrator * */ public interface MemberDao { /** * 获取所有用户列表 * @return */ public List<MembersModel> findAllMembersModels(); }
com.spring.dao.imp 新建一个MemberDaoImpl 实现类
package com.spring.dao.impl; import java.util.List; import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; import com.spring.dao.MemberDao; import com.spring.model.MembersModel; /** * @author Administrator * */ public class MemberDaoImpl extends SimpleJdbcDaoSupport implements MemberDao { /* (non-Javadoc) * @see com.spring.MemberDao#findAllMembersModels() */ public List<MembersModel> findAllMembersModels() { // 去数据库里查询 //sql.... return null; } }
com.spring.dao.model 新建一个MembersModel类
package com.spirng.model; /** * @author Administrator * */ public class MembersModel { private String id ; private String logonname; private String logonpassword; public MembersModel(){ } public MembersModel(String id,String logonname,String logonpassword){ this.id=id; this.logonname=logonname; this.logonpassword=logonpassword; } public String getLogonpassword() { return logonpassword; } public void setLogonpassword(String logonpassword) { this.logonpassword = logonpassword; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getLogonname() { return logonname; } public void setLogonname(String logonname) { this.logonname = logonname; } }
com.spring.service 新建一个MemberService接口
package com.spring.service; import java.util.List; import com.spring.model.MembersModel; /** * @author Administrator * */ public interface MemberService { /** * 返回所有用户列表 * @return 用户列表 */ public List<MembersModel> findAllMembersModels(); }
com.spring.service.impl 新建一个MemberServiceImpl实现类
package com.spring.service.impl; import java.util.List; import com.spring.dao.MemberDao; import com.spring.model.MembersModel; import com.spring.service.MemberService; /** * @author Administrator * */ public class MemberServiceImpl implements MemberService { private MemberDao memberDao; public void setMemberDao(MemberDao memberDao) { this.memberDao = memberDao; } /* (non-Javadoc) * @see com.spring.service.MemberService#findAllMembersModels() */ public List<MembersModel> findAllMembersModels() { // TODO Auto-generated method stub return memberDao.findAllMembersModels(); } }
com.spring.web.controller 新建一个MemberController 控制类
package com.spring.web.controller; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import com.spring.model.MembersModel; import com.spring.service.MemberService; /** * @author Administrator * */ public class MemberController extends MultiActionController { private MemberService memberService; public void setMemberService(MemberService memberService) { this.memberService = memberService; } public ModelAndView memberList(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException{ //查询数据库了 List<MembersModel> list = memberService.findAllMembersModels(); return new ModelAndView("MemberList").addObject("memberlist", list); } }
以上就是我src下的结构,遵循spring的mvc模式
4.修改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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--对WEB-INF下的xml文件进行整合,本来spring就一个配置文件,但是为了可读性,我把他分开了--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 把所以页面的字符编码转换成utf-8 --> <filter> <filter-name>encodingFilter</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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 一个页面请求控制器,相当于struts的web.xml的拦截器 --> <servlet> <servlet-name>court</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>court</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> </web-app>
5.在WEB-INF下配置spring的配置文件
为了增强配置文件的可读性,新生容易理解一点,我这里把配置文件分开了,有些配置文件是空的,主要为了以后业务拓展
(1)Copy of court-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-autowire="byName"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/MemberList.htm">memberController</prop> </props> </property> </bean> <bean id="memberController" class="com.spring.web.controller.MemberController"> <property name="methodNameResolver"> <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/MemberList.htm">memberList</prop> </props> </property> </bean> </property> </bean> </beans>
(2)spring-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-autowire="byName"> <bean id="memberDao" class="com.spring.dao.impl.MemberDaoImpl"></bean> </beans>
(3)spring-dwr.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:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd" default-autowire="byName"> </beans>
(4)spring-persistence.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" default-autowire="byName"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" /> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.2.151.137:1521:orcl" /> <property name="user" value="dexs" /> <property name="password" value="dexs" /> <!-- <property name="minPoolSize" value="10" /> <property name="maxPoolSize" value="20" /> <property name="maxIdleTime" value="1800" /> <property name="acquireIncrement" value="2" /> <property name="maxStatements" value="0" /> <property name="initialPoolSize" value="2" /> <property name="idleConnectionTestPeriod" value="1800" /> <property name="acquireRetryAttempts" value="30" /> <property name="breakAfterAcquireFailure" value="true" /> <property name="testConnectionOnCheckout" value="false" /> --> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ></bean> </beans>
(5)spring-service.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-autowire="byName"> <bean id="memberService" class="com.spring.service.impl.MemberServiceImpl"></bean> </beans>
(6)views.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> </beans>
7.部署工程,进行测试
感谢大家对我支持