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
*
*
备注:公众号清汤袭人能找到我,那是随笔的地方
备注:公众号清汤袭人能找到我,那是随笔的地方