Spring MVC hello world例子程序

来源:互联网 发布:还珠格格天上人间知画 编辑:程序博客网 时间:2024/05/29 11:18

在eclipse下创建一个dynamic project,

然后在WebContent目录下创建lib文件夹,导入所需jar包



注:mysql-connector-java可以不导入。

在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TalkBox</display-name>
 
  <!-- 配置DispatchcerServlet -->
  <servlet>
   <servlet-name>springDispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 配置Spring mvc下的配置文件的位置和名称 -->
   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
   </init-param>
   
   <!-- 将启动加载优先级设置为0,容器在应用启动时就加载并初始化这个servlet -->
   <load-on-startup>0</load-on-startup>
  </servlet>
 
  <servlet-mapping>
   <servlet-name>springDispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


在src目录下创建springmvc.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
          
         
         <!-- 配置自动扫描的包 -->
         <context:component-scan base-package="com.fwk.springmvc"></context:component-scan>
         
         <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name = "prefix" value="../"></property>
             <property name = "suffix" value = ".jsp"></property>
         </bean>
 </beans>


src文件夹下新建com.fwk.springmvc包,包下新建HelloWorld.java:

package com.fwk.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {
    
    @RequestMapping("/helloworld")
    public String Hello(){
        System.out.println("hello world");
        return "success";
    }
}


再在webContent
目录下新建index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld">hello world</a>
</body>
</html>


以及success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功
</body>
</html>


然后运行在服务器上即可

共勉

0 0