SpringMVC 注解Demo

在web. xml中添加

01.<context-param>
02. <param-name>contextConfigLocation</param-name>
03. <param-value>/WEB-INF/applicationContext.xml</param-value>
04. </context-param>
05. <listener>
06. <listener-class>
07. org.springframework.web.context.ContextLoaderListener
08. </listener-class>
09. </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


01.<?xml version="1.0" encoding="UTF-8"?>
02.
03. <beans xmlns="http://www.springframework.org/schema/beans"
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. <context:annotation-config />
24.
25. <context:component-scan base-package="org.app.demo.spring" /> <!-- 自动扫描所有注解该路径 -->
26.
27.</beans>


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

01.<?xml version="1.0" encoding="UTF-8"?>
02.
03. <beans xmlns="http://www.springframework.org/schema/beans"
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. <context:annotation-config />
20.
21. <!-- 把标记了@Controller注解的类转换为bean -->
22.
23. <context:component-scan base-package="org.app.demo.spring.controller" />
24.
25. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
26.
27. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
28.
29.
30.
31. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
32.
33. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
34.
35. p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
36.
37.
38.
39. <bean id="multipartResolver"
40.
41. class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
42.
43. p:defaultEncoding="utf-8" />
44.
45. </beans>


在源目录下新建三个包


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.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
06.
07.<html>
08.
09.<head>
10.
11.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12.
13.<title>Insert title here</title>
14.
15.<link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">
16.
17.</head>
18.
19.<body>
20.
21.<form action="helloworld.do" method="post">
22.
23.请输入姓名:<input type="text" name="userName" />
24.
25.<input type="submit" value="提交" />
26.
27.<br />
28.
29.</form>
30.
31.</body>
32.
33.</html>


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


01.<%@ page language="java" contentType="text/html; charset=UTF-8"
02.
03. pageEncoding="UTF-8"%>
04.
05.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
06.
07.<html>
08.
09.<head>
10.
11.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12.
13.<title>Insert title here</title>
14.
15.<link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">
16.
17.</head>
18.
19.<body>
20.
21.<h1><%=request.getAttribute("newUserName")%></h1>
22.
23.</body>
24.
25.</html>


保存完后布置到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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值