springmvc简单示例
類結構圖:
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
轉載于:https://www.cnblogs.com/springmvc-hibernate/archive/2012/02/09/2483975.html
總結
以上是生活随笔為你收集整理的springmvc简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wp7技术类网站汇总
- 下一篇: 高性能 HTML5 地铁样式的应用程序中