SPRINGMVC Learning (annotation-based MVC)

Source: Internet
Author: User

Today this blog to bring you a note-based SPRINGMVC development, have you ever thought of a problem, that is, we have previously written based on the configuration file configuration controller can only default processing a method, Then I imagine structs2 to let a controller can handle multiple requests, for different requests, jump into different methods to do processing, this is excellent, SPRINGMVC based on the development of annotations, you can complete the above requirements, let's look at an example.

Add Jar File

First we need to add the required jar file, here is just a com.springsource.javax.annotation-1.0.0.jar more than the previous article Springmvc HelloWorld, as follows:
Com.springsource.javax.annotation-1.0.0.jar
Com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
Com.springsource.org.aopalliance-1.0.0.jar
Com.springsource.org.apache.commons.logging-1.1.1.jar
Com.springsource.org.apache.taglibs.standard-1.1.2.jar
Org.springframework.aop-3.0.0.release.jar
Org.springframework.asm-3.0.0.release.jar
Org.springframework.beans-3.0.0.release.jar
Org.springframework.context-3.0.0.release.jar
Org.springframework.context.support-3.0.0.release.jar
Org.springframework.core-3.0.0.release.jar
Org.springframework.expression-3.0.0.release.jar
Org.springframework.web-3.0.0.release.jar
Org.springframework.web.servlet-3.0.0.release.jar

Configure Spring's core distributor

Next we need to configure our Spring core Distributor to add the following in Web. xml:

<servlet>    <servlet-name>Spring</servlet-name>    <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>            <param-name>Contextconfiglocation</param-name>            <param-value>Classpath:spring-servlet.xml</param-value>        </init-param></servlet><servlet-mapping>    <servlet-name>Spring</servlet-name>    <url-pattern>/</url-pattern> </servlet-mapping>

Here I change the name of the core dispatcher to spring and initialize the parameter contextconfiglocation under the classpath, so we need to create a file named: Spring-servlet.xml under SRC.

Create a Spring-servlet.xml file
<?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/sprin G-beans-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.sprin                         Gframework.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.S                         PRINGFRAMEWORK.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 ">    <mvc:annotation-driven/>    <context:component-scan base-package="Com.test.spring.zhujie"></context:component-scan>    <Beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver" >                < property name="prefix" value="/web-inf/pages/" ></Property >        < property name="suffix" value=". jsp"></Property >    </Bean></Beans>

This is different from the previous blog, adding a "note driver" and "Note Driver Scan package", that is, when configured here, the annotations under the package will not take effect.

<mvc:annotation-driven/>    <context:component-scan base-package="com.test.spring.zhujie">    </context:component-scan>

About the Viewresolver configuration is the same, there is not much to say ha.
Next I create a new indexontroller, and add annotations:

Packagecom. Test. Spring. Zhujie;import org. Springframework. Stereotype. Controller;import org. Springframework. Web. Bind. Annotation. Requestmapping;@Controller @requestmapping (value="/index") public class Indexcontroller {@RequestMapping (value="/add") Public StringAdd() {System. out. println("Add runs ....");Return"Index";}}

@Controller used to label the class as a controller, so you don't have to inherit from the various controllers as before, @RequestMapping (value= "/index") means I Can "/index" Such URLs are accessed to that class. Looking at the Add () method, the @RequestMapping (value= "/add") represents the subpath, that is, if you want to jump to the method to handle it, you must add the root path, such as here I want to let the Add method to handle, the URL can write "http/ Loalhost:8080/springmvc2/index/add ", this will be handed to the method to deal with.
In the Add method, we return "index", combined with the configured Viewresolver, this return value will jump to "web-inf/pages/index.jsp", we can find that the annotation-based configuration, can save a lot of code, And in a controller class, you can handle multiple methods.

So what if I need to pass the parameters?? Here I'm writing a method that receives an ID of type int.

@RequestMapping(value="remove")    publicremove(int id) {        System.out.println("remove runs .... id is :"+id);        return"remove";}

At this point, I access through the URL, you can directly get the parameters passed over:
"Http://localhost:8080/springmvc2/index/remove?id=3"
It can be found that as long as the parameter names that I declare are the same as the parameters passed, they are available.

What if I have a lot of parameters?? Do you still declare a number of parameters in the method?? Here I'm creating a new method:

@RequestMapping(value="delete")publicdelete(Person person) {        System.out.println("the person is :"+person);        return"delete";}

You can see that I declare a type of person parameter in the method, and then only need to access it through such a URL, you can get the parameters passed by the browser side.
Http://localhost:8080/springmvc2/index/delete?id=2&name=uuu&pass=iii
Note that the parameter names passed here need to be the same as the attributes declared in the person class.

Another traditional way to get a parameter is to declare a parameter of type HttpServletRequest in the method. Here I'm defining a method:

@RequestMapping(value="update")publicupdate(HttpServletRequest request) {        String param = request.getParameter("param");        System.out.println("the param is :"+param);        return"update";}

You can see here that the arguments passed by are obtained directly from the request parameter of the Declaration, at which point the parameters can be passed through such URLs: http://localhost:8080/springmvc2/index/update?param=88

So what if I just want my method to handle get requests??
That's all you have to say.
@RequestMapping (value= "Add", Method=requestmethod.get)

Well, today's SPRINGMVC is based on the implementation of annotations, and here it is.
SOURCE download

SPRINGMVC Learning (annotation-based MVC)

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.