现在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)
分享到:
相关推荐
4. **J2EE框架**:熟悉Spring、SpringMVC、Struts2、Hibernate和Mybatis,这些都是常见的Java企业级应用框架,用于简化开发、实现依赖注入、事务管理和持久化。 5. **Spring框架深入理解**:理解Spring的IOC(依赖...
掌握主流的Java开源框架Struts2、Spring、SpringMVC、Hibernate/Mybatis、iBatis等,最好熟悉Freemaker或volecity。 2. 熟悉中间件Tomcat、jboss、Apache、Weblogic、WAS; 3. 熟悉各种Web前端技术,包括JavaScript...
wangtengfei-hn_EmployeesExample_23540_1745868671962
scratch少儿编程逻辑思维游戏源码-汽车冲突.zip
scratch少儿编程逻辑思维游戏源码-棱镜.zip
少儿编程scratch项目源代码文件案例素材-直升机坠毁.zip
输入法优化与定制_五笔编码编辑与词库管理_Rime输入法引擎与86极点码表_跨平台五笔码表编辑器工具_for_macOS与Windows系统_支持用户自定义词条添加删除与排序_提供
少儿编程scratch项目源代码文件案例素材-主题乐园大亨.zip
scratch少儿编程逻辑思维游戏源码-迷失在像素平原.zip
少儿编程scratch项目源代码文件案例素材-纸格通关 云变量.zip
wanjunshe_Python-Tensorflow_12888_1745868924470
scratch少儿编程逻辑思维游戏源码-深入海底.zip
驾校自动化_网页自动化爬虫技术_Python27多线程HTTP请求模拟_龙泉驾校2014版约车系统自动预约助手_通过模拟登录和循环请求实现自动约车功能_支持失败自动递增车号重试_
scratch少儿编程逻辑思维游戏源码-南瓜危机.zip
scratch少儿编程逻辑思维游戏源码-皮博冒险者.zip
基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档~ 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档
用于释放电脑的内存,很好用。
scratch少儿编程逻辑思维游戏源码-气球足球.zip
ollama 0.6.6.0官网下载,不方便的可以从这里下载