类结构图:
jar包:
Webroot目录:
1.==============================
package com.strive.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.strive.service.TestService;
public class TestController implements Controller{
/**
* 测试数据服务类
*/
private TestService testService;
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("TestController.handlerRequest()");
request.setAttribute("info", "Hello World! HCML");//设置变量
this.testService.add(request.getParameter("name"));//添加用户信息
return new ModelAndView("index");//指定跳转的视图名称
}
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
}
2.====================
package com.strive.dao;
import com.strive.entity.Test;
public class TestDao {
public void add(Test test ){
System.out.println("TestDao.add(Test.class)");
}
}
3.===========================
package com.strive.entity;
public class Test {
//编号
private Integer id;
//测试名称
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.=============================================
package com.strive.service;
import com.strive.dao.TestDao;
import com.strive.entity.Test;
public class TestService {
//测试实体的dao类
private TestDao testDao;
public void add(String name){
System.out.println("TestService.add()");
Test test=new Test();
test.setId(10);
test.setName(name);
this.testDao.add(test);//添加数据到数据库中
}
public TestDao getTestDao() {
return testDao;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
}
5.==============================================dao-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="testDao" class="com.strive.dao.TestDao"></bean>
</beans>
6==================================================service-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- test数据服务类 -->
<bean id="testService" class="com.strive.service.TestService">
<property name="testDao" ref="testDao"></property>
</bean>
</beans>
7================================================web-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- Controller方法调用规则定义 -->
<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
<property name="defaultMethodName" value="list"></property>
</bean>
<!-- 页面view层基本信息设置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- servlet映射表,所有控制层Controller的servlet在这里定义 -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="test.do">testController</prop>
</props>
</property>
</bean>
<!-- Controller控制器定义 -->
<bean id="testController" class="com.strive.controller.TestController">
<property name="testService" ref="testService"></property>
</bean>
</beans>
8===================================================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">
<!-- servlet 的控制器配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-config.xml,/WEB-INF/service-config.xml,/WEB-INF/dao-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置访问的时候的后缀 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
9=====================================================index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
${info }
</body>
</html>
访问地址:http://localhost:8080/springmvc_01/test.do?name=HC
分享到:
相关推荐
SpringMVC简单示例,简洁明了,不同于ssh的集成,但却一看就懂
Spring MVC中使用Swagger生成API文档和完整项目示例Demo
SpringMvc 简单例子,入门级教程,你值得拥有。容易上手。
基于注解的springMVC简单的例子,基于注解的springMVC简单的例子。
springmvc简单样例代码,可以参考一下
springMVC的简单示例,同时讲到了如何在servlet中获取springBean 是一个很好的学习springmvc 的开始资料
java spring mvc超简单示例,适合入门
SpringMVC+myBatis入门简单示例分享,轻量级 SpringMVC入门+myBatis简单示例分享
springmvc4简单例子,springmvc例子,适合学习。。。。。。
示例是很有用的,这里列举的是非常简单的小java项目。应用了spring mvc,学习本项目可以了解前怎么调用的后端?后端怎么提供的接口?怎么增加的依赖等。 教程见:https://www.cnblogs.com/daguozb/p/12491310.html
Maven+SpringMVC的入门级HelloWorld实例,简单了解Maven和SpringMVC整合
基础spring mvc示例,包含基本的控制层代码,以及spring mvc、web.xml配置,和一个最简单的index.html
SSM spring + springMVC +myBatis 非maven 源代码 ,再详细不过的,简单入门的 ssm 框架 SSM SSM源代码 SSM SSM源代码
spring初学这可以参考的用户登录注册简单的例子,mvc设计模式的spring框架,适合初学者,比较简单,容易理解和入门
一个简单地基于spring+springmvc+hibernate框架的web开发,帮助搭建项目。仅供参考。
spring+springmvc+mybatis 简单实例 实现简单的界面 主要包含各种配置
4、本资料只是一个很简单的spring-mvc搭建的工程,旨在让初学者简单了解spring-mvc的配置,能给初学者一个在代码中实践的简单的平台。 5、代码可直接运行,没有任何业务处理,清晰明了(适合对spring有一定了解的...
Spring+SpringMVC+Mybatis最简单的整合示例,希望对大家有所帮助。
springMVC+Mybaits+dwr示例,用 SpringMVC+myBaits+dwr写的简单的小例子.