现在springMVC的整合吧
第一步:web.xml的配置
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
dwr的配置()
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class> org.directwebremoting.spring.DwrSpringServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
第二步:WEB-INF/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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 定义一个视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
第三步: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:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 激活spring的注解. -->
<context:annotation-config />
<!-- 扫描注解组件并且自动的注入spring beans中.
例如,他会扫描@Controller 和@Service下的文件.所以确保此base-package设置正确. -->
<context:component-scan base-package="net.rytong" />
<!-- 配置注解驱动的Spring MVC Controller 的编程模型.注:次标签只在 Spring MVC工作! -->
<mvc:annotation-driven />
<mvc:default-servlet-handler default-servlet-name="IndexServlet"/>
<!-- ********************************************** -->
<!-- dwr启用 annotation 配置模式 -->
<dwr:configuration>
<dwr:convert type="bean" class="net.rytong.entity.Person" />
</dwr:configuration>
<!-- pring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类 -->
<dwr:annotation-config />
<!-- 开启debug -->
<dwr:controller id="dwrController" debug="true" />
<!-- ********************************************** -->
<!-- 导入jdbc的配置文件 -->
<import resource="jdbc-context.xml" />
</beans>
第四步:数据源jdbc-context.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- <context:property-placeholder location="classpath:db.properties" /> -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="driverUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maximumConnectionCount" value="300" />
<property name="minimumConnectionCount" value="10" />
<property name="prototypeCount" value="4" />
<property name="maximumActiveTime" value="600000" />
<property name="simultaneousBuildThrottle" value="5" />
<property name="testBeforeUse" value="true" />
<property name="houseKeepingTestSql" value="SELECT CURRENT_DATE" />
</bean>
<!-- 定义jdbc配置信息路径 -->
<context:property-placeholder location="/WEB-INF/db.properties" />
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 定义事务管理 -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
</beans>
dao的代码:
@Repository("personDao")
public class PersonDao {
private SimpleJdbcTemplate jdbcTemplate;
@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
public List<Person> getAllPerson() {
String sql = "select id, first_name, last_name, money from person";
// Maps a SQL result to a Java object
RowMapper<Person> mapper = new RowMapper<Person>() {
public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
Person person = new Person();
person.setId(rs.getInt("id"));
person.setFirstName(rs.getString("first_name"));
person.setLastName(rs.getString("last_name"));
person.setMoney(rs.getDouble("money"));
return person;
}
};
return jdbcTemplate.query(sql, mapper);
}
}
service中的代码:
@Service("personService")
@RemoteProxy(name="PersonService")
public class PersonService {
protected static Logger logger = Logger.getLogger("service");
@Autowired
private PersonDao personDao;
/**
*检索所有的Person
*/
@RemoteMethod
public List<Person> getAll() {
logger.debug("Retrieving all persons");
System.out.println(personDao+"==========");
return personDao.getAllPerson();
}
}
control中的代码:
@Controller
@RequestMapping("/main")
public class MainController {
protected static Logger logger = Logger.getLogger("controller");
@Resource(name="personService")
private PersonService personService;
/**
*获得所有的Person并返回到指定JSP页面
*
* @return the name of the JSP page
*/
@RequestMapping(value = "/persons.do", method = RequestMethod.GET)
public String getPersons(Model model) {
logger.debug("Received request to show all persons");
// 调用personService中的getAll获得所有的Person
List<Person> persons = personService.getAll();
// 把Person装入一个指定的model
model.addAttribute("persons", persons);
// 解析 /WEB-INF/jsp/personspage.jsp
return "personspage";
}
要想servlet要spring容器来管理,在web.xml中 去掉springMVC配置 加上如下配置:
<servlet>
<servlet-name>UserLoginServlet</servlet-name>
<servlet-class>net.rytong.servlet.ServletProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserLoginServlet</servlet-name>
<url-pattern>/UserLoginServlet</url-pattern>
</servlet-mapping>
并加上这个代理类
@SuppressWarnings("serial")
public class ServletProxy extends GenericServlet {
private Servlet proxy;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
proxy.service(req, res);
}
@Override
public void init() throws ServletException {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
System.out.println(getServletName()+"===============");
this.proxy = (Servlet) wac.getBean(getServletName());
proxy.init(getServletConfig());
}
}
具体代码下载:( 数据库是基于mysql,jar包管理是Maven 下载后报错请修改pom.xml)
分享到:
相关推荐
自己做得简易 spring+springMvc+jdbc整合,可改进内容还很多,仅仅是个demo,tomcat使用的8.0
jquery+springmvc+springjdbc+sprngquartz整合java项目 jquery-easyui-1.2.6 spring3.1.1
spring+springmvc+springjdbc+redis。作者自己的作品。包含了当前最新的redis
这个dome运用了spring3+springMVC+springJDBC+jQuery easyui三个后台框架和一个前台框架,主要还有例子
idea+gralde+spring+springmvc+springjdbc+mysql实现单表的增删改查
spring mvc + spring + hibernate 全注解整合开发视频教程 11
这个dome是运用了spring3+springMVC3+springJDBC3+jQuery easyui三个后台框架加上一个前台框架组件的
Spring+Spring MVC+Spring JDBC+MySql实现简单登录注册
这里就要导入mybatis和spring的整合包了,这里sqlsession中也要导入mybatis的配置文件 2.6 spring-bean 配置mapper自动扫描 MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子类), ...
Spring mvc + Spring + Spring jdbc 整合实例源码
Spring mvc + Spring + Spring jdbc 整合 demo
采用IDE idea 创建的gradle项目,项目框架为 spring+springMVC+springJDBC 实现单表的insert,update,delete ,getList等操作,一个小小的demo()
maven+spring+jdbc+mvc项目整合,代码容易简单 有详细的说明。
一、项目简述 功能: 用户和企业用户的注册登录,简历的筛选查看搜索,应聘信息互动等等。 二、项目运行 ...Jdbc+ Servlert + SpringMvc + Jsp + css + JavaScript + JQuery + Ajax + Fileupload等等
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
基于JDBC+JSP+Servlet的图书管理系统基于JDBC+JSP+Servlet的图书管理系统 基于JDBC+JSP+Servlet的图书管理系统基于JDBC+JSP+Servlet的图书管理系统 基于JDBC+JSP+Servlet的图书管理系统基于JDBC+JSP+Servlet的图书...
基于JDBC+MySQL+Servlet+JSP+Java实现简单校园论坛系统 基于JDBC+MySQL+Servlet+JSP+Java实现简单校园论坛系统 基于JDBC+MySQL+Servlet+JSP+Java实现简单校园论坛系统 基于JDBC+MySQL+Servlet+JSP+Java实现简单校园...