一、servlet xml配置
1.首先建立一个maven项目。
2.导入servlet的依赖包servlet-api 和jsp-api
<!--添加Servlet和JSP依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
3.因为浏览默认情况是不能servlet资源的 所以为配置web.xml 即配置servlet 和 servlet-mapping 通过servlet映射方式来来处理浏览器请求
<servlet>
<servlet-name>myFirstServlet</servlet-name>
<servlet-class>info.minghui.web.MyFirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myFirstServlet</servlet-name>
<url-pattern>/myFirst</url-pattern>
</servlet-mapping>
<filter>
<filter-name>character</filter-name>
<filter-class>info.minghui.web.MyFilter</filter-class>
</filter>
若要配置字符集过滤器 可以在web.xml中配置全局字符集然后通过"/ "映射过滤所有请求
<context-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>
<filter>
<filter-name>character</filter-name>
<filter-class>info.minghui.web.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>character</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
过滤器类:
package info.minghui.web;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding=filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;character="+encoding);
chain.doFilter(request,response);
}
public void destroy() { }
}
二、servlet 注解配置
自动servlet3.0,servlet的开发逐渐使用注解配置,省去大量的xml配置文件
在使用注解配置的时候我们需要更换servlet-api版本
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
而之后,我们把web.xml文件删除(此时不再需要xml配置文件了)
只需要把serlvet类上加上注解@WebServlet ,过滤器类上面加上@WebFilter即可
@WebServlet("/myFirst")
public class MyFirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
System.out.println("使用了get请求-----");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@WebFilter("/")
public class MyFilter implements Filter {
private String encoding;
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding=filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setContentType("text/html;character="+encoding);
chain.doFilter(request,response);
}
public void destroy() { }
}
三、SpringMVC纯XML配置
因为springmvc中所有的请求都交给了DispatcherServlet管理所以先注册一个DispathcherServlet并加载springmvc配置文件springmvc.xml
web.xml中的配置如下图
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--记载springmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动就加载该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- '/' 匹配所有的请求不包括jsp,在springmvc配置文件中我们在配置其他静态资源放行的问题-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在resouces下建立一个spring.xml文件.springmvc.xml中的配置如下图:
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--静态资源放行问题 location表示webapp下的目录-->
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images"></mvc:resources>
<!--处理器解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="WEB-INF/jsp/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--BeanNameUrlHandlerMapping处理器需要根据id进行处理请求-->
<bean id="/hello" class="info.ming.controller.MyXMLController"></bean>
</beans>
然后在编写一个MyXMLController类实现Controller接口
public class MyXMLController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "hello");
mv.setViewName("test");
return mv;
}
}
四、SpringMVC半注解配置
其中web.xml中的配置和纯XML配置一样
其spring.xml中的配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="info.ming"></context:component-scan>
<!--让springmvc不处理静态资源.css .js .html .mp3 .mp4 即让这些资源不走视图解析器-->
<mvc:default-servlet-handler/>
<!--支持mvc注解驱动
spring中一般采用@RequestMapping中注解来完成映射关系要想使@RequestMapping注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapet实例
这两个类实例分别在类级别和方法级别处理
而annotation-driven配置帮助我们自动完成两个实例的注入
-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
contoller类中的写法
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("msg", "hello MyController la la la ");
return "hello";
}
}
五:SpringMVC纯注解配置
编写springmvc.xml对应的配置类
@Configuration
@ComponentScan("info.ming")
@EnableWebMvc
public class MyConfig {
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver irv = new InternalResourceViewResolver();
irv.setSuffix(".jsp");
irv.setPrefix("/WEB-INF/jsp/");
return irv;
}
}
编写启动类
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext aca = new AnnotationConfigWebApplicationContext();
aca.register(MyConfig.class);
aca.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(aca));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
}
}