Spring mvc--Build Step (note) ☆ Common Way

Source: Internet
Author: User

☆ Common Way

1. Build a Web Project

2. Importing the JAR Package

Spring-beans.jar, Spring-context.jar, Spring-core.jar, Spring-expression.jar, Spring-aop.jar, Spring-web.jar, Spring-webmvc.jar "

Add Dependency Packages: Commons-logging.jar, Aopalliance.jar

3. Configuring the front-end controller Dispatcherservlet in Web. xml: Handling requests and responses

<!--configuration Dispatcherservlet: Responsible for processing requests and responses
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--configuration Spring-mvc.xml--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value><!--class Paths--
<!--<param-value>/WEB-INF/spring-mvc.xml</param-value> relative path, default path:/web-inf/springmvc-servlet.xml- -
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

★★★★★ Note: You need to configure the initialization parameter contextconfiglocation at the same time to verify that the Spring-mvc.xml configuration file is loaded while initializing the Dispatcherservlet ★★★★

4. Create a Spring-mvc.xml file in the classpath, configure the "annotations" processor adapter, processor Mapper

<!--1. The annotations processor adapter--
<!--<bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > </bean>

<!--1. The Annotations processor Mapper--
<!--<bean class= "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > </bean>

<!--2. This way you can replace the configuration adapter, mapper statement, and
<mvc:annotation-driven></mvc:annotation-driven>

Note: When using this method, it is important to note that the header of the XML file needs to be changed as follows:

<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:mvc= "Http://www.springframework.org/schema/mvc"
xsi:schemalocation= "Http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
Http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">

5. Writing the processor through the "annotations" method

@Controller//Represents the class as a controller/processor
public class hellocontroller{

@RequestMapping ("/getallemp")//Represents the method access path, relative to the project root
Public Modelandview getallemp ()
{
System.out.println ("Hellocontroller.getallemp () ....");

list<emp> emplist = new arraylist<emp> ();
Emplist.add (New EMP (1000, "Zhang San", "Sales"));
Emplist.add (New Emp (2000, "John Doe", "staff"));
Emplist.add (New EMP (3000, "Harry", "manager"));

Modelandview Modelandview = new Modelandview ();

Modelandview.addobject ("Emplist", emplist);

Modelandview.setviewname ("main.jsp");

return modelandview;
}

}

6. In the Spring-mvc.xml file, configure the component scanner to load all processors (scan all classes with class-level annotations, such as @controller)

<!--processor--
<!--<bean id= "Hellocontroller" class= "controller. Hellocontroller "></bean>

<!--configuration Components scanner: Manages all beans under the controller package, the above bean definitions can be omitted--
<context:component-scan base-package= "Controller" ></context:component-scan>

7. Writing the View main.jsp
<table>
<tr>
<th> numbering </th>
<th> name </th>
<th> Jobs </th>
</tr>
<c:foreach items= "${emplist}" var= "EMP" >
<tr>
<td>${emp.empno}</td>
<td>${emp.ename}</td>
<td>${emp.job}</td>
</tr>
</c:forEach>
</table>

8. In the Spring-mvc.xml file, configure the View resolver

<!--View Resolver--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ></bean>

9. Test path

Http://localhost:8088/springMvc_01_annotation/getAllEmp.action

Spring mvc--Build Step (note) ☆ Common way

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.