Spring web mvc

Source: Internet
Author: User

As a code, you need to know more about programming. It is a process of gradual accumulation. I recently learned about spring web mvc and wrote down some of my personal experiences.

 

1. Prepare the jar package. Spring mvc has reached version 4, and many materials on the Internet are no longer applicable. It cannot be used either. This is very painful. I spent a lot of time getting these JAR packages.

Get http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#overview-distribution-zip in official introduction documents

Download here: http://repo.spring.io/release/org/springframework/spring/4.0.1.RELEASE/

You also need the jstl package, which you can search.

2. Create a web project and place the jar package under the lib of the project's WEB-INF. For example:

3. Create 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>Web Application</display-name>  <servlet>    <servlet-name>dispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/conf/spring/spring-servlet.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>dispatcherServlet</servlet-name>    <url-pattern>*.htm</url-pattern>  </servlet-mapping>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/conf/spring/spring-servlet.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener></web-app>

  

4. Create the folder WEB-INF/conf/spring, and then create the file spring-servlet.xml content as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Configure the xml file parsing template required by the SpringMVC configuration file --> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: cxf = "http://cxf.apache.org/core" xmlns: tx = "http://www.springframework.org/schema/tx" 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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http: // Www.springframework.org/schema/tx/spring-tx.xsd "> <! -- Configure the package path for the Controller to be traced --> <context: component-scan base-package = "zyf. demo"/> <! -- Configure the parser --> <bean id = "jspViewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" viewClass "value =" org. springframework. web. servlet. view. jstlView "/> <! -- Parser parsing/WEB-INF/jsp/path under,. jsp end of the zookeeper file --> <property name = "prefix" value = "/WEB-INF/jsp/"/> <property name = "suffix" value = ". jsp "/> </bean> </beans>

 

5. Write Controller

Package zyf. demo; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; @ Controller @ RequestMapping ("/demo") public class HelloWorldController {@ RequestMapping ("/helloWorld") // url field name public String helloWorld () {return "helloWorld "; // jsp file name }}

6. Write 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"><%    request.getRequestDispatcher("/demo/helloWorld.htm").forward(request, response);%>

 

Success. More time is wasted on finding the jar package.

 

 

 

 

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.