SpringMVC 注解Demo

在web. xml中添加

01.
02. contextConfigLocation
03. /WEB-INF/applicationContext.xml
04.

05.
06.
07. org.springframework.web.context.ContextLoaderListener
08.

09.

10.
11. spring
12.
13. org.springframework.web.servlet.DispatcherServlet
14.

15. 1
16.

17.
18. spring
19. *.do
20.

21.
22. Encoding
23.
24. org.springframework.web.filter.CharacterEncodingFilter
25.

26.
27. encoding
28. utf8
29.

30.

31.
32. Encoding
33. /*
34.



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


01.
02.
03. 04.
05. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
06.
07. xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
08.
09. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10.
11. xsi:schemaLocation="
12.
13. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
14.
15. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
16.
17. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
18.
19. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
20.
21.
22.
23.
24.
25.
26.
27.



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

01.
02.
03. 04.
05. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
06.
07. xmlns:context="http://www.springframework.org/schema/context"
08.
09. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10.
11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12.
13. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14.
15. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33. 34.
35. p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
36.
37.
38.
39. 40.
41. class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
42.
43. p:defaultEncoding="utf-8" />
44.
45.



在源目录下新建三个包


01.org.app.demo.spring.controller
02.
03.org.app.demo.spring.service
04.
05.org.app.demo.spring.service.impl


在controller包下建HelloWorldController类


01.package org.app.demo.spring.controller;
02.
03.
04.
05.import javax.servlet.http.HttpServletRequest;
06.
07.import org.app.demo.spring.service.HelloWorldService;
08.
09.import org.springframework.beans.factory.annotation.Autowired;
10.
11.import org.springframework.stereotype.Controller;
12.
13.import org.springframework.web.bind.annotation.RequestMapping;
14.
15.import org.springframework.web.bind.annotation.RequestParam;
16.
17.
18.
19.@Controller
20.
21.@RequestMapping("/helloworld.do")
22.
23.public class HelloWorldController{
24.
25.
26.
27. @Autowired
28.
29. private HelloWorldService helloWorldService;
30.
31.
32.
33. @RequestMapping
34.
35. public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){
36.
37. String newUserName = helloWorldService.getNewName(userName);
38.
39. request.setAttribute("newUserName", newUserName);
40.
41. return "helloworld";
42.
43. }
44.
45.}


在service包下新建HelloWorldService接口


01.package org.app.demo.spring.service;
02.
03.public interface HelloWorldService {
04.
05. public String getNewName(String userName);
06.
07.}
08.
09.在impl包下新建HelloWorldService接口的实现类HelloWorldServiceImpl类
10.package org.app.demo.spring.service.impl;
11.
12.import org.app.demo.spring.service.HelloWorldService;
13.
14.import org.springframework.stereotype.Service;
15.
16.import org.springframework.transaction.annotation.Transactional;
17.
18.@Service
19.
20.public class HelloWorldServiceImpl implements HelloWorldService {
21.
22. @Override
23.
24. @Transactional
25.
26. public String getNewName(String userName) {
27.
28. return "Hello Spring!" + userName;
29.
30. }
31.
32.}


新建index.jsp


01.<%@ page language="java" contentType="text/html; charset=UTF-8"
02.
03. pageEncoding="UTF-8"%>
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.Insert title here
14.
15.
16.
17.
18.
19.
20.
21.

22.
23.请输入姓名:
24.
25.
26.
27.

28.
29.

30.
31.
32.
33.


然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.jsp


01.<%@ page language="java" contentType="text/html; charset=UTF-8"
02.
03. pageEncoding="UTF-8"%>
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.Insert title here
14.
15.
16.
17.
18.
19.
20.
21.

<%=request.getAttribute("newUserName")%>


22.
23.
24.
25.


保存完后布置到Tomcat中,启动Tomcat,访问http://localhost:8080/myapp/index.jsp

输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do。
显示Hello Spring!张三。一切OK


引用页面:[url]http://blog.csdn.net/hepeng154833488/article/details/6653032[/url]

你可能感兴趣的:(SpringMVC)