Spring MVC Getting Started configuration

Source: Internet
Author: User

1. Copy the required jar to the project directory Web-inf/lib

2. Configure Web. XML To configure the front controller Org.springframework.web.servlet.DispatcherServlet

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring_config/applicationContext-mvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

3. Create a Spring MVC control core configuration: Applicationcontext-mvc.xml

Configuring the Controller

<bean id= "Login" name= "/login.do" class= "Com.beifeng.controller.LoginController" ></bean>

Configuration Attempt Parser

<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >

<!--prefixes--

<property name= "prefix" value= "/jsp/" ></property>

<!--suffix--

<property name= "suffix" value= ". JSP" ></property>

</bean>

4. Write the controller class Logincontroller must inherit Abstractcontroller rewrite handlerequestinternal

@Override

protected Modelandview handlerequestinternal (HttpServletRequest request,

HttpServletResponse arg1) throws Exception {

TODO auto-generated Method stub

String userName = Request.getparameter ("UserName");

String USERPW = Request.getparameter ("USERPW");

System. out. println ("------I am SPRINGMVC controller---------");

System. out. println ("account number:" +username);

System. out. println ("account number:" +USERPW);

Modelandview Mav = new modelandview ("index");

Analog Call Model

if ("admin". Equals (UserName) && "123". Equals (USERPW)) {

Mav.addobject ("message", "User login successful!");

}Else{

Mav.addobject ("message", "Incorrect account or password!") ");

}

return Mav;

}

Returns data and attempts to modelandview Mav = new Modelandview ("index");

5. Writing JSP page display data

Use {$message} in the JSP interface to display the message variable

Spring MVC Getting Started configuration

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.