Simple examples of SpringMVC and SpringMVC

Source: Internet
Author: User

Simple examples of SpringMVC and SpringMVC

First, import the required jar package. The project directory structure is as follows:

Configure the web. xml file as follows:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_2_5.xsd" version="2.5"> 3   <servlet> 4     <servlet-name>springDispatcherServlet</servlet-name> 5     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 6     <init-param> 7       <param-name>contextConfigLocation</param-name> 8       <param-value>classpath:applicationContext.xml</param-value> 9     </init-param>10     <load-on-startup>1</load-on-startup>11   </servlet>12   <servlet-mapping>13     <servlet-name>springDispatcherServlet</servlet-name>14     <url-pattern>/</url-pattern>15   </servlet-mapping>16 </web-app>

Then configure applicationContext. xml:

1 <beans xmlns = "http://www.springframework.org/schema/beans" 2 xmlns: context = "http://www.springframework.org/schema/context" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/sch Ema/context/spring-context-3.0.xsd "> 9 10 <! -- Configure the automatically scanned package --> 11 <context: component-scan base-package = "com. sprmvc. po"> </context: component-scan> 12 <! -- Configure the view parser to resolve the returned values in HelloWorldController to the actual physical view --> 13 <bean14 class = "org. springframework. web. servlet. view. InternalResourceViewResolver"> 15 <! -- Prefix is the prefix, and suffix is the suffix --> 16 <property name = "prefix" value = "/"> </property> 17 <property name = "suffix" value = ". jsp "> </property> 18 </bean> 19 </beans>

Create object class User. java as follows:

 1 package com.sprmvc.po; 2  3 public class User { 4     private String userName; 5     private String password; 6     public String getUserName() { 7         return userName; 8     } 9     public void setUserName(String userName) {10         this.userName = userName;11     }12     public String getPassword() {13         return password;14     }15     public void setPassword(String password) {16         this.password = password;17     }18 }

Then there is the control layer code:

 1 package com.sprmvc.po; 2  3 import javax.servlet.http.HttpServletRequest; 4  5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8  9 @Controller10 public class HelloWorldController {11     @RequestMapping(value="/hello",method = RequestMethod.GET)12     public String printHelloWorld(HttpServletRequest request,User user) {13         request.setAttribute("userName", user.getUserName());14         request.setAttribute("password", user.getPassword());15         return "hello";16     }17 }

The value here is the access path, and the "hello" of return is returned to hello. jsp through the view parser configured in applicationContext. xml.

Next we will create two. jsp pages, first index. jsp:

1 <body> 2 <form method = "get" action = "hello"> 3 userName: <input type = "text" name = "userName"> 4 password: <input type = "password" name = "password"> <br> 5 <input type = "submit" value = "submit"> 6 </form> 7 </body>

Here, the method and action correspond to the values of the method and value in the control layer respectively, that is, the 11th rows in HelloWorldController. java.

Then hello. jsp:

1 <body> 2 

In this way, you only need to load the project to tomcat for access. Please try again.

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.