springmvcdemo2

首先去Spring官網上去下載最新的Spring版本。http://www.springsource.com/download/community

目前最新的版本是3.1 M2。下載完後解壓,將dist目錄下的所有jar文件複製到你的項目的lib目錄下(我在MyEclipse中新建了一個myapp的Web項目),另外再添加如下的JAR包,
commons-fileupload-1.2.1.jar

commons-logging-1.1.1.jar

在web. xml中添加:

  1. <context-param>

  2. <param-name>contextConfigLocation</param-name>

  3. <param-value>/WEB-INF/applicationContext.xml</param-value>

  4. </context-param>

  5. <listener>

  6. <listener-class>

  7. org.springframework.web.context.ContextLoaderListener

  8. </listener-class>

  9. </listener>

  10. <servlet>

  11. <servlet-name>spring</servlet-name>

  12. <servlet-class>

  13. org.springframework.web.servlet.DispatcherServlet

  14. </servlet-class>

  15. <load-on-startup>1</load-on-startup>

  16. </servlet>

  17. <servlet-mapping>

  18. <servlet-name>spring</servlet-name>

  19. <url-pattern>*.do</url-pattern>

  20. </servlet-mapping>

  21. <filter>

  22. <filter-name>Encoding</filter-name>

  23. <filter-class>

  24. org.springframework.web.filter.CharacterEncodingFilter

  25. </filter-class>

  26. <init-param>

  27. <param-name>encoding</param-name>

  28. <param-value>utf8</param-value>

  29. </init-param>

  30. </filter>

  31. <filter-mapping>

  32. <filter-name>Encoding</filter-name>

  33. <url-pattern>/*</url-pattern>

  34. </filter-mapping>


另外在WEB-INF下新建applicationContext.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

  4. xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"

  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  6. xsi:schemaLocation="

  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  11. <context:annotation-config/>

  12. <context:component-scanbase-package="org.app.demo.spring"/><!-- 自動掃描所有註解該路徑 -->

  13. </beans>


在WEB-INF下新建spring-servlet.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  9. <context:annotation-config/>

  10. <!-- 把標記了@Controller註解的類轉換爲bean -->

  11. <context:component-scanbase-package="org.app.demo.spring.controller"/>

  12. <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->

  13. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

  14. <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->

  15. <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"

  16. p:prefix="/WEB-INF/views/"p:suffix=".jsp"/>

  17. <beanid="multipartResolver"

  18. class="org.springframework.web.multipart.commons.CommonsMultipartResolver"

  19. p:defaultEncoding="utf-8"/>

  20. </beans>


在源目錄下新建三個包

  1. org.app.demo.spring.controller

  2. org.app.demo.spring.service

  3. org.app.demo.spring.service.impl


在controller包下建HelloWorldController類

  1. package org.app.demo.spring.controller;

  2. import javax.servlet.http.HttpServletRequest;

  3. import org.app.demo.spring.service.HelloWorldService;

  4. import org.springframework.beans.factory.annotation.Autowired;

  5. import org.springframework.stereotype.Controller;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RequestParam;

  8. @Controller

  9. @RequestMapping("/helloworld.do")

  10. publicclass HelloWorldController{

  11. @Autowired

  12. private HelloWorldService helloWorldService;

  13. @RequestMapping

  14. public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){

  15. String newUserName = helloWorldService.getNewName(userName);

  16. request.setAttribute("newUserName", newUserName);

  17. return"helloworld";

  18. }

  19. }


在service包下新建HelloWorldService接口

  1. package org.app.demo.spring.service;

  2. publicinterface HelloWorldService {

  3. public String getNewName(String userName);

  4. }

  5. 在impl包下新建HelloWorldService接口的實現類HelloWorldServiceImpl類

  6. package org.app.demo.spring.service.impl;

  7. import org.app.demo.spring.service.HelloWorldService;

  8. import org.springframework.stereotype.Service;

  9. import org.springframework.transaction.annotation.Transactional;

  10. @Service

  11. publicclass HelloWorldServiceImpl implements HelloWorldService {

  12. @Override

  13. @Transactional

  14. public String getNewName(String userName) {

  15. return"Hello Spring!" + userName;

  16. }

  17. }


新建index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  4. <html>

  5. <head>

  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  7. <title>Insert title here</title>

  8. <link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">

  9. </head>

  10. <body>

  11. <form action="helloworld.do" method="post">

  12. 請輸入姓名:<input type="text" name="userName" />

  13. <input type="submit" value="提交" />

  14. <br />

  15. </form>

  16. </body>

  17. </html>


然後再WEB-INF目錄下新建views目錄,在views目錄下新建helloworld.jsp

  1. <%@ page language="java"contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  4. <html>

  5. <head>

  6. <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

  7. <title>Insert title here</title>

  8. <linkrel="stylesheet"type="text/css"href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">

  9. </head>

  10. <body>

  11. <h1><%=request.getAttribute("newUserName")%></h1>

  12. </body>

  13. </html>


保存完後佈置到Tomcat中,啓動Tomcat,訪問http://localhost:8080/myapp/index.jsp

輸入姓名(如張三)後,頁面會跳轉到http://localhost:8080/myapp/helloworld.do
顯示Hello Spring!張三。一切OK!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章