Note-based spring MVC (required jar package, Web. XML configuration, Spring file configuration, @Controller, @RequestMapping, @RequestParam, model, El Value)

Source: Internet
Author: User

1. Add jar

2. Web. XML configuration:

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

>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.springmvc.xml Configuration

<?

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:mvc= "Http://www.springframework.org/schema/mvc"
Xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/AOP
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--note-driven
<mvc:annotation-driven/>
<!--components Scan--
<context:component-scan base-package= "Cn.itcast.springmvc.controller" ></context:component-scan>

<!--Configure Internal Resource View Resolver--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/jsp/" ></property>
<property name= "suffix" value= ". JSP" ></property>
</bean>
</beans>

4. Entity Bean

Package cn.itcast.springmvc.domain;

public class User {

private String name;
Private String address;
Private Integer age;
Private String Tel;

Public String GetName () {
return name;
}

public void SetName (String name) {
System.out.println ("The value of name is being injected by the SetName method:" + name);
THIS.name = name;
}

Public String getaddress () {
return address;
}

public void setaddress (String address) {
this.address = address;
}

Public Integer Getage () {
return age;
}

public void Setage (Integer age) {
This.age = age;
}

Public String Gettel () {
return Tel;
}

public void Settel (String tel) {
This.tel = Tel;
}

@Override
Public String toString () {
Return ' {name: ' + name + ', Address: ' + address + ', Age: ' + age '
+ ", Tel:" + Tel + "}";
}

}

5, write HomeController, code such as the following:

Package Cn.itcast.springmvc.controller;

Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;

Import Cn.itcast.springmvc.domain.User;

/**
* @brief Iaccountdao.java Learn Spring annotation method
* @attention
* @author coating rights
* @date 2014-5-18
* @note begin modify NULL
*/
@Controller//Add annotations
@RequestMapping (value = "Home")//root path. Some namespaces that resemble Strut2
public class HomeController {

/**
* Sub-path, indicating that only get commits are supported
* @param req can get the number of references by passing HttpServletRequest
* @param name indicates where the connection is: xxx?name=
* @param u assume URL? There are too many later, to get the number of references. Can write this parameter directly to the user
* @param model: Defines a map object that can be passed to a JSP page in this way
*
* @attention URL address can be: Http://localhost:8081/SpringMVC_02/home/hello
* NAME=TOTO&AMP;ADDRESS=HAIDIAN&AMP;AGE=24&AMP;TEL=136XXX
* The number of parameters obtained is: Running the Hello method Name:toto User: {name:toto,address:haidian,age:24,tel:136xxx}
* @return
*/
@RequestMapping (value= "/hello", Method=requestmethod.get)
Public String Hello (httpservletrequest req,
@RequestParam (value = "Name")
String name, User u, map<string, object> model) {
String name = Req.getparameter ("name");
System.out.println ("Running Hello Method Name:" + name);
System.out.println ("User:" + u);
Req.setattribute ("msg", "Hello" + name);
Model.put ("msg", "Hello" + name);
return "Hello";//Logical name
}

/**
* \brief Definition Method hi
*
* @return
* @attention URL where to access the address by/home/hi
* @author coating rights
* @date 2014-5-18
* @note begin modify by null
*/
@RequestMapping (value= "/hi")/Sub-path
Public String hi () {
SYSTEM.OUT.PRINTLN ("Running Hi Method");
return "HI"; Logical name
}
}

6, the written hello.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

<title> ' hello.jsp ' </title>

<body>
${REQUESTSCOPE.MSG}
</body>

Note-based spring MVC (required jar package, Web. XML configuration, Spring file configuration, @Controller, @RequestMapping, @RequestParam, model, El Value)

Related Article

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.