springmvcdemo2
2013-11-19 10:18
344 查看
首先去Spring官网上去下载最新的Spring版本。http://www.springsource.com/download/community目前最新的版本是3.1 M2。下载完后解压,将dist目录下的所有jar文件复制到你的项目的lib目录下(我在MyEclipse中新建了一个myapp的Web项目),另外再添加如下的JAR包,
commons-fileupload-1.2.1.jarcommons-logging-1.1.1.jar在web. xml中添加:[html]view plaincopyprint?<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
另外在WEB-INF下新建applicationContext.xml[html]view plaincopyprint?<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scanbase-package="org.app.demo.spring"/><!-- 自动扫描所有注解该路径 -->
</beans>
在WEB-INF下新建spring-servlet.xml[html]view plaincopyprint?<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
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/aop http://www.springframework.org/schema/aop/spring-aop-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:annotation-config/>
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scanbase-package="org.app.demo.spring.controller"/>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/"p:suffix=".jsp"/>
<beanid="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8"/>
</beans>
在源目录下新建三个包
[java]view plaincopyprint?org.app.demo.spring.controller
org.app.demo.spring.service
org.app.demo.spring.service.impl
在controller包下建HelloWorldController类
[java]view plaincopyprint?package org.app.demo.spring.controller;
import javax.servlet.http.HttpServletRequest;
import org.app.demo.spring.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/helloworld.do")
publicclass HelloWorldController{
@Autowired
private HelloWorldService helloWorldService;
@RequestMapping
public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){
String newUserName = helloWorldService.getNewName(userName);
request.setAttribute("newUserName", newUserName);
return"helloworld";
}
}
在service包下新建HelloWorldService接口
[java]view plaincopyprint?package org.app.demo.spring.service;
publicinterface HelloWorldService {
public String getNewName(String userName);
}
在impl包下新建HelloWorldService接口的实现类HelloWorldServiceImpl类
package org.app.demo.spring.service.impl;
import org.app.demo.spring.service.HelloWorldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
publicclass HelloWorldServiceImpl implements HelloWorldService {
@Override
@Transactional
public String getNewName(String userName) {
return"Hello Spring!" + userName;
}
}
新建index.jsp[plain]view plaincopyprint?<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">
</head>
<body>
<form action="helloworld.do" method="post">
请输入姓名:<input type="text" name="userName" />
<input type="submit" value="提交" />
<br />
</form>
</body>
</html>
然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.jsp[html]view plaincopyprint?<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
<linkrel="stylesheet"type="text/css"href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">
</head>
<body>
<h1><%=request.getAttribute("newUserName")%></h1>
</body>
</html>
保存完后布置到Tomcat中,启动Tomcat,访问http://localhost:8080/myapp/index.jsp输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do。
显示Hello Spring!张三。一切OK!
commons-fileupload-1.2.1.jarcommons-logging-1.1.1.jar在web. xml中添加:[html]view plaincopyprint?<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
另外在WEB-INF下新建applicationContext.xml[html]view plaincopyprint?<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scanbase-package="org.app.demo.spring"/><!-- 自动扫描所有注解该路径 -->
</beans>
在WEB-INF下新建spring-servlet.xml[html]view plaincopyprint?<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
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/aop http://www.springframework.org/schema/aop/spring-aop-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:annotation-config/>
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scanbase-package="org.app.demo.spring.controller"/>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/"p:suffix=".jsp"/>
<beanid="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8"/>
</beans>
在源目录下新建三个包
[java]view plaincopyprint?org.app.demo.spring.controller
org.app.demo.spring.service
org.app.demo.spring.service.impl
在controller包下建HelloWorldController类
[java]view plaincopyprint?package org.app.demo.spring.controller;
import javax.servlet.http.HttpServletRequest;
import org.app.demo.spring.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/helloworld.do")
publicclass HelloWorldController{
@Autowired
private HelloWorldService helloWorldService;
@RequestMapping
public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){
String newUserName = helloWorldService.getNewName(userName);
request.setAttribute("newUserName", newUserName);
return"helloworld";
}
}
在service包下新建HelloWorldService接口
[java]view plaincopyprint?package org.app.demo.spring.service;
publicinterface HelloWorldService {
public String getNewName(String userName);
}
在impl包下新建HelloWorldService接口的实现类HelloWorldServiceImpl类
package org.app.demo.spring.service.impl;
import org.app.demo.spring.service.HelloWorldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
publicclass HelloWorldServiceImpl implements HelloWorldService {
@Override
@Transactional
public String getNewName(String userName) {
return"Hello Spring!" + userName;
}
}
新建index.jsp[plain]view plaincopyprint?<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">
</head>
<body>
<form action="helloworld.do" method="post">
请输入姓名:<input type="text" name="userName" />
<input type="submit" value="提交" />
<br />
</form>
</body>
</html>
然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.jsp[html]view plaincopyprint?<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
<linkrel="stylesheet"type="text/css"href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">
</head>
<body>
<h1><%=request.getAttribute("newUserName")%></h1>
</body>
</html>
保存完后布置到Tomcat中,启动Tomcat,访问http://localhost:8080/myapp/index.jsp输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do。
显示Hello Spring!张三。一切OK!
相关文章推荐
- SpringMVC接收json数据的demo
- springmvc整合dubbo分布式系统的搭建demo
- 基于springMVC文件上传的例子。ajax提交表单,子窗口关闭刷新父界面的demo
- Maven,SpringMVC小demo实例
- SpringMVC整合Freemarker(含Demo源码)
- Springmvc注解注入的简单demo
- springMVC 与mybatis 整合 demo(maven 工程)
- SpringMVC-Spring-Hibernate项目搭建之一-- 搭建maven 项目 & servlet的demo
- springMVC demo
- SpringMVC学习(一)小demo
- Spring,SpringMVC和hibernate整合小demo
- Spring 学习笔记4—— SpringMVC的控制器与View简单demo
- 图文详解springMVC demo的配置及运行过程
- Springmvc注解注入的简单demo
- Springmvc入门教程,结合Ajax实现最佳精简demo,直击原理,方便易用。
- maven+IDEA+springmvc小demo理解springmvc
- 【SSH系列】一步步深入springmvc+商品列表查询demo
- springMvc入门Demo例子
- springmvc rest风格的实例代码demo讲解
- 自己跑通的一个SpringMVC配合Hibernate的demo,写下来,免得以后再跳坑