SpringMVC and springmvc
SpringMVC framework setup steps:
1. Import the required jar package to the lib folder. 2. Configure web. xml. 3. Configure the springMVC core configuration file. 4. encode the Controller class.
Note: the source code of this project is imported into eclipse. After tomcat runs, enter http: // localhost: 8080/BrainTrain/welcome. jsp for testing.
Required jar package: http://pan.baidu.com/s/1i3QKYNF (Baidu cloud disk)
Project source code: http://pan.baidu.com/s/1kTWM9Rh; (Baidu cloud disk)
1. Configure web. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5">
<Display-name> BrainTrain </display-name>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
<Servlet>
<Servlet-name> springMVC </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<! -- SpringMVC configuration file address. config is the package under src -->
<Param-value> classpath *: config/springAnnotation-servlet.xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> springMVC </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>
</Web-app>
2. Spring MVC core configuration file: springAnnotation-servlet.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: p = "http://www.springframework.org/schema/p"
Xmlns: mvc = "http://www.springframework.org/schema/mvc"
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.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd>
<! -- Annotation scan package -->
<Context: component-scan base-package = "com. chuck. codeResource"> </context: component-scan>
<! -- Enable annotation -->
<Mvc: annotation-driven/>
<! -- Static resource access -->
<Mvc: resources location = "/img/" mapping = "/img/**"/>
<Mvc: resources location = "/js/" mapping = "/js/**"/>
<Mvc: resources location = "/staticHtml/" mapping = "/staticHtml/**"/>
<Bean id = "viewResolver" class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "prefix" value = "/"> </property>
<Property name = "suffix" value = ". jsp"> </property>
</Bean>
</Beans>
3. Compile the Controller class
Package com. chuck. codeResource. user;
Import java. util. HashMap;
Import java. util. Map;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import org. springframework. stereotype. Controller;
Import org. springframework. web. bind. annotation. RequestMapping;
Import org. springframework. web. bind. annotation. RequestMethod;
Import org. springframework. web. servlet. ModelAndView;
Import org. springframework. web. servlet. mvc. multiaction. MultiActionController;
@ Controller
@ RequestMapping ("/user ")
Public class LoginController extends MultiActionController {
@ RequestMapping ("/addUser ")
Public String addUser (HttpServletRequest request, HttpServletResponse response ){
System. out. println ("----- add ----");
String result = "this is addUser ";
Request. setAttribute ("result", result );
Return "/welcome ";
}
@ RequestMapping ("/delUser ")
Public String delUser (HttpServletRequest request, HttpServletResponse response ){
System. out. println ("----- delUser ----");
String result = "this is delUser ";
Request. setAttribute ("result", result );
Return "/welcome ";
}
@ RequestMapping ("/updateUser ")
Public String updateUser (HttpServletRequest request, HttpServletResponse response ){
System. out. println ("----- update ----");
String result = "this is updateUser ";
Request. setAttribute ("result", result );
Return "/welcome ";
}
}
4. Configure the accessed jsp page: welcome. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> Insert title here </title>
</Head>
<Body>
The method for this execution is: $ {result}
<Form action = "user/addUser" method = "post">
<Input type = "submit" value = "post"/>
</Form>
</Body>
</Html>