第一节:springmvc的helloworld实例
2015-08-22 22:11
495 查看
方法一:
方法2:
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/example/*</url-pattern> </servlet-mapping> </web-app>
HelloWorld-servlet.xml(必需放在WEB-INF文件夹下) <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="zwxu" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
HelloController.java(访问地址为http://localhost:8080/项目名称/example/index) @Controller @RequestMapping("/index") public class HelloController{ @RequestMapping(method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "index"; } }
附:WebController.java
<pre name="code" class="java">@Controller(每个方法对应一个地址) public class WebController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { <span style="white-space:pre"> </span> return "hello"; } @RequestMapping(value = "/redirect", method = RequestMethod.GET) public String redirect() { return "redirect:finalPage"; //forward:finalPage } @RequestMapping(value = "/finalPage", method = RequestMethod.GET) public String finalPage() { return "final"; } }
</pre><pre name="code" class="html">index.html <%@ page contentType="text/html; charset=UTF-8" %> <html> <head> <title>Hello World</title> </head> <body> <h2>${message}</h2> </body> </html>
方法2:
更改web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/*.xml</param-value> //此时*可任意取名称,且*.xml的位置可由开发者任意指定 </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/example/*</url-pattern> </servlet-mapping> </web-app>
相关文章推荐
- java-多线程1
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- spring JDBC 查询没有目标的方法
- spring启动流程
- java高级特性和核心优化gc
- 学渣级别springmvc初探
- Error initializing Java Runtime Environment. You may need to reinstall Flash
- Java 源码学习线路————_先JDK工具包集合_再core包,也就是String、StringBuffer等_Java IO类库
- Spring学习笔记-spring基础知识概述
- java并发编程---ThreadLocal
- Java安全管理器(Security Manager)
- Java之旅hibernate(1)——初始
- 开发工具之Eclipse快捷键
- Jetty学习(三)--jetty部署spring mvc项目
- Java记录 -7- 流程控制语句续
- Struts2学习(二)执行Action中方法的三种方式