Spring入門實踐

   This artical won`t build a wonder environment. actually, i try to make it into a smaller one with the condition that it can run on a sever like atomcat.  & now i have no intent to enhance features or add more functions to it. i have to sorry that the picture shotcut troubles me a lot , so few pictures will paste on this artical to illustrate clearly.   

     prepare:

     you should at least know how to install & uninstall & configure java、tomcat、idea; if you have learned a lot about servlet & spring, that`s good, you can skip this package.

    step 1: build a maven project

    here is a example:

 

step 2: package to war

in the pom.xml under <version>1.0-SNAPSHOT</version> write like this <packaging>war</packaging>

step 3 : add dependencies & dependency javax.servlet  > javax.servlet-api  org.springframework > spring-web & spring-webmvc

pom examples:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>palette.song</groupId>
    <artifactId>gotta</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

    </dependencies>

</project>

step 4 : modify project structure 

File > Project Structure > Modules... 

first you can set you project directoy (add the directory manually) like this :

then chose paths , change classes file path into you created classes file path。Apply then click OK

step 5: add web.xml  under the path WEB-INF/ 

web.xml content & project structure fellows :

step 6: create a simple controller and config your spring-servlet.xml. here runs:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.cloud"></context:component-scan>



</beans>
package com.cloud.song;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class HelloWorld {

    @RequestMapping("/hi")
    public void sayHello(){

    }

}

step 6: run on a server, like tomcat. run goes here:

things goes as we like, so far we have build a small & simple spring web program, if you see the INTERNAL SERVER ERROR 500, don`t worry, just add HttpServletRequest request or HttpServletResponse response or both of them above or both of them  above & other params, one can like this:

public void sayHello(HttpServletRequest request, HttpServletResponse response)

Everything goes here. So sorry i have no sufficient time to dig on this project. 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章