springMVC的HelloWorld

来源:互联网 发布:cosplay的软件 编辑:程序博客网 时间:2024/06/05 16:18

1、lib包

2、项目结构

3、web.xml配置

1) <!-- 配置Spring配置文件的加载路径 -->

2) <!--<context-param>

3) <param-name>contextConfigLocation</param-name>

4) <param-value>/WEB-INF/applicationContext.xml</param-value>

5) </context-param> -->

6) 

7) <!-- 配置Spring容器的监听器,它将使用上配置的contextConfigLocation的值的路径来获得Spring配置文件的路径  -->

8) <!-- <listener>

9) <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

10) </listener> -->

11)   

12)   <!-- springMVC configuration -->

13)   <servlet>    

14)     <servlet-name>dispatcherServlet</servlet-name>    

15)     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   

16)     <init-param>

17)       <param-name>contextConfigLocation</param-name>

18)       <param-value>/WEB-INF/springmvc/springMVC_servlet.xml</param-value>

19)     </init-param> 

20)     <load-on-startup>1</load-on-startup>    

21)   </servlet>    

22)   <servlet-mapping>    

23)     <servlet-name>dispatcherServlet</servlet-name>    

24)     <url-pattern>/</url-pattern>    

25)   </servlet-mapping>  

4、spirngMVC的xml配置,文件为springMVC_servlet.xml

1) <?xml version="1.0" encoding="UTF-8"?>

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

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

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

5)  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  

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

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

 

8) <!-- 自动扫描的包名 -->

9) <context:component-scan base-package="org.spring.helloworld"/>

10) <!-- 默认的注解映射的支持 -->

11) <mvc:annotation-driven />

12) <!-- 视图解释类 -->

13) <bean

14)  class="org.springframework.web.servlet.view.InternalResourceViewResolver">

15) <property name="prefix" value="/WEB-INF/jsp/" />

16) <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->

17) <property name="suffix" value=".jsp" />

18) </bean>

 

19) </beans>  

5、SpringHelloWorld.java文件

1) package org.spring.helloworld;

 

2) import java.util.HashMap;

3) import java.util.Map;

 

4) import org.springframework.stereotype.Controller;

5) import org.springframework.ui.Model;

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

 

7) @Controller

8) public class SpringHelloWorld {

9) @RequestMapping(value="/hello")//访问的地址,如:http://localhost:8080/项目名/hello

10) public String hello(){

11) System.out.println("这是spring的HelloWorld");

12) return "hello";//跳转到页面hello.jsp

13) }

14) @RequestMapping(value="/add/demo")

15) public Map<String, String> add(){

16) System.out.println("这是加法方法");

17) Map<String,String> map =new HashMap<String, String>();

18) map.put("key","你好");//springMVC框架中将值返回到页面,页面中得到此值的方法是${key}。

19) return map;//与没有返回值得方法一样,跳转到页面demo.jsp

20) }

21) 

22) @RequestMapping(value="/subtract")

23) public void subtract(Model model){

24) model.addAttribute("a","Joy");//这是返回到桌面值,页面得到值得方法是${a}

25) System.out.println("这是加法方法");

26) //void,请求什么地址,跳转到什么地址,此方法跳转到subtract.jsp

27) }

 

28) }

6、demo.jsp

1) <body>

2)   这是测试   ${key}

3) </body>


0 0