1.springmvc basic configuration
Import the jar package
Configure the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sprinmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springm</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 过滤器的配置 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Among them, configure the front controller, associate the springmvc-servlet.xml file, and set the interception request; the listener can be configured or not configured to intercept all pages, process encoding, prevent Chinese garbled, and uniformly change it to utf-8 encoding.
Configure springmvc-servlet.xml file: configure Springmvc automatic scanner, view resolver
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 配置处理器 -->
<!-- <bean id="login" name="/login.do" class="com.controller.LoginController"></bean>-->
<!-- 配置普通类 -->
<!-- <bean id="annotationLogin" class="com.controller.AnnotationLoginController"></bean>-->
<!-- 注册注解驱动 -->
<!-- <mvc:annotation-driven/>-->
<!-- <mvc:annotation-driven/> -->
<!-- 配置springmvc的自动扫描器 -->
<context:component-scan base-package="com.controller"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Note: When the automatic scanner is configured, there is no need to configure the processor and ordinary classes. The automatic scanner will automatically scan all classes under the package and identify those with annotations: when using, declare the classes under the package as Controller with annotations. Note Path matching
@Controller
public class HelloWordController {
@RequestMapping("/helloword")
public String helloword(Model model){
model.addAttribute("message","StringMvc 来了");
return "helloword";
}
The name of the page returned by return is matched by the view parser.
In this way, the simple configuration of springmvc is complete. You can perform a simple jump experiment through the front-end page
<a href="helloword.do">springmvc</a>