Configure SPRINGMVC projects in a myeclipse environment for simple requests

Source: Internet
Author: User

1. "File-->new-->webproject"Create a newWebproject, named Springmvc

2. Copy the jar package you need to the Web-inf/lib path, then right-click the project, build path-->configure build Path, select Add JARs in the Libraries tab, Select the jar package under Webcontent/web-inf/lib in the SPRINGMVC project.

3. Create a new myjsp.jsp file under the webcontent root directory, code as:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "

pageencoding= "UTF-8"%>

<% @taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<body>

Welcome everyone to watch my video! Accelerated Li Shouhong

<br/>

<div>

<c:foreach items= "${map}" var= "M" >

${m.key}-----> ${m.value}<br/>

</c:forEach>

</div>

</body>

Create the Web. xml file under 4.webcontent/web-inf, with the following code:

<?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 "id=" webapp_id "version=" 2.5 ">

<display-name>springMVC1</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>springMVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:config/spring-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springMVC</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

Create the Com.tgb.web.controller package in the 5.SRC directory,

There are Mycontroller.java, the code is as follows:

Package Com.tgb.web.controller;

Import java.lang.annotation.Annotation;

Import Java.util.HashMap;

Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import Org.springframework.ui.Model;

Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.servlet.ModelAndView;

Import Org.springframework.web.servlet.mvc.Controller;

public class Mycontroller implements Controller {

@Override

Public Modelandview HandleRequest (HttpServletRequest arg0,

HttpServletResponse arg1) throws Exception {

TODO auto-generated Method Stub

Map<string,object> map=new hashmap<string,object> ();

Map.put ("121544123", "Jianxiaoling");

Map.put ("121544101", "Dong");

Model.addattribute ("map", map);

return "/myjsp";

return new Modelandview ("/myjsp", "map", map);

}

}

There are Mycontrollerannotation.java, the code is as follows:

Package Com.tgb.web.controller;

Import Java.util.HashMap;

Import Java.util.Map;

Import Org.springframework.stereotype.Controller;

Import Org.springframework.ui.Model;

Import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class Mycontrollerannotation {

@RequestMapping ("/annotation")

Public String Hi (model model) {

Map<string,object> map=new hashmap<string,object> ();

Map.put ("121544123", "Jianxiaoling");

Map.put ("121544101", "Dong");

Model.addattribute ("map", map);

return "/myjsp";

}

}

6. In the SRC directory, create a new config package, create a new Spring-servlet.xml file with the following code:

<?xml version= "1.0" encoding= "UTF-8"?>

<beans xmlns= "Http://www.springframework.org/schema/beans"

xmlns:context= "Http://www.springframework.org/schema/context"

xmlns:p= "http://www.springframework.org/schema/p"

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

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.xsd

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

<mvc:annotation-driven/>

<context:component-scan base-package= "Com.tgb.web.controller"/>

<bean id= "UrlMapping"

class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >

<property name= "Mappings" >

<props>

<prop key= "/hello" >helloaction </prop> <!--map URL Access--

</props>

</property>

</bean>

<bean name= "helloaction" class= "Com.tgb.web.controller.MyController"/>

<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >

<property name= "prefix" value= "/" ></property>

<property name= "suffix" value= ". JSP" ></property>

</bean>

</beans>

The project structure looks like this:

Configure SPRINGMVC projects in a myeclipse environment for simple requests

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.