SpringMVC简单入门-HelloWorld

*

参考地址:http://www.cnblogs.com/sunada2005/p/3576431.html#commentform

 小例子的结构

1,web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="citic" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>SpringMVC</display-name>

      
      <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
      
      <welcome-file-list>
          <welcome-file>/jsp/HelloWorld.jsp</welcome-file>
      </welcome-file-list>
</web-app>

2,SpringMVC-servlet.xml, *-servlet.xml,此处*填写web.xml中ServletName的值

<?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:p="http://www.springframework.org/schema/p"    
    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-3.0.xsd     
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context-3.0.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
           
     <!-- http://192.168.1.117:8080/SpringMVC/test1/helloWorld -->
    <bean name="/test1/helloWorld" class="com.springmvc.web.controller.HelloWorldController" /> 
     
    <!-- ViewResolver --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- find jsp from WEB-INF folder
       <property name="prefix" value="/WEB-INF"/> 
        --> 
        <!-- find jsp from WebContent/jsp folder -->
        <property name="prefix" value="/jsp"/> 
        <property name="suffix" value=".jsp"/> 
    </bean>
     
</beans>

 3,HelloWorldController.java

package com.springmvc.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("hello,this is HelloWorldController.java");
        
        /**
         * SpringMVC-servlet.xml中
         * <property name="prefix" value="/"/> 则从webContent下面读jsp
         * <property name="prefix" value="/WEB-INF"/> 从WEB-INF文件夹下读jsp
         */
        return new ModelAndView("/HelloWorld");//对应的就是HelloWorld.jsp
    }

}

4, HelloWorld.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>Hello World</title>
</head>
<body>
This is HelloWorld.jsp, /WebContent/jsp 文件夹下面
</body>
</html>

访问:http://localhost:8080/SpringMVC/test1/helloWorld

 

*

转载于:https://www.cnblogs.com/qingmaple/p/5224489.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值