SpringMVC_HelloWorld_01, springmvchelloworld

Source: Internet
Author: User

SpringMVC_HelloWorld_01, springmvchelloworld

Implement a simple HelloWorld through the configuration file.

1. Create a project 1. Create a dynamic web project

2. Named engineering springmvc-01

3. Check "Generate web. xml deployment descriptor"

4. Import the jar package

5. Create a springmvc configuration file

6. name the configuration file springmvc-servlet.xml, naming rules: <servlet-name>-servlet. xml

7. Select a namespace

8. Complete

Ii. Configuration File 1. Configure 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_3_0.xsd"    id="WebApp_ID" version="3.0">  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- Map all requests to the DispatcherServlet for handling -->    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>
2. Configure springmvc-servlet.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 h Ttp: // www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd "> <! -- Configure HandlerMapping and find the corresponding Controller according to BeanName --> <bean class = "org. springframework. web. servlet. mvc. support. ControllerBeanNameHandlerMapping"> </bean> <! -- Configure the Controller and respond to the mvc request --> <bean name = "/mvc" class = "com. zhy. controllers. HelloWorldController"> </bean> <! -- Configure the view parser --> <bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> <! -- Configure prefix --> <property name = "prefix" value = "/WEB-INF/views/"> </property> <! -- Configure the suffix --> <property name = "suffix" value = ". jsp"> </property> </bean> </beans>
3. Compile the Controller
Package com. zhy. controllers; 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 {/** use the ViewResolver to obtain the actual physical view * parsing rules of the view Parser: prefix + viewname + suffix * combined with this instance, the physical view parsed by the view parser is:/WEB-INF/views/helloworld. jsp **/@ Override public ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {// TODO Auto-generated method stub System. out. println ("call controller"); return new ModelAndView ("helloworld ");}}
4, new jsp page 1, new helloworld. jsp, path: WEB-INF/views/helloworld. jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">2. Create index. jsp, path: WEB-INF/index. jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">3. Complete

Iv. Run

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.