spring-mvc.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:p="http://www.springframework.org/schema/p"
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-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.xsd">
<!-- 开启spring容器的自动扫描功能 -->
<context:component-scan base-package="com.ge.springmvcanno"></context:component-scan>
<!-- 开启springmvc的注解支持 -->
<mvc:annotation-driven enable-matrix-variables="true"/>
<!-- 指定静态资源文件的位置 -->
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
<!-- 不再需要配置处理映射器,因为框架会自动使用 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
去根据我们定义的@RequestMapping该注解,完成"请求路径"到"处理该请求方法"之间的映射关系 -->
<!-- 不再需要配置处理适配器,因为框架会自动使用 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
去根据我们定义的@RequestMapping该注解,完成"Servlet数据"到"Controller数据"之间的数据适配 -->
<!-- 配置视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml配置:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!-- 配置前端控制器:
1、接收用户的请求,并完成请求的派发
2、实例容器,并且完成配置文件转交给spring容器 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
结合注解的表现层:(只需要加入@RequestMapping()注解就可以了)
@RequestMapping在类上表示定义命名空间,@RequestMapping在方法上表示具体的位置,请求就可以以“/sys/login”这样子访问到了
@RequestMapping("/sys")
@Controller
public class LoginController {
private Logger log = Logger.getLogger(this.getClass());
@RequestMapping(value = "/login")
public ModelAndView login(HttpServletRequest req, HttpServletResponse res) {
String loginName = req.getParameter("loginName");
String password = req.getParameter("password");
log.info(loginName);
log.info(password);
ModelAndView mv = new ModelAndView("/layout/main");
return mv;
}
@RequestMapping(value = "/login02")
public ModelAndView login02(HttpServletRequest req,HttpServletResponse res) {
String loginName = req.getParameter("loginName");
String password = req.getParameter("password");
log.info(loginName);
log.info(password);
ModelAndView mv = new ModelAndView("/layout/main");
return mv;
}
}