环境:eclipse+tomcat8.5
第一步:jar准备(基础包):
commons-logging-1.1.1.jar
jstl-1.2.jar
spring-aop-4.0.4.RELEASE.jar
spring-aspects-4.2.6.RELEASE.jar
spring-beans-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
第二步:web.xml文件配置拦截器
servlet-name可以随意命名,上下必须一致
第三步:配置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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 开启注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler />
<!-- 扫描java文件路径 -->
<context:component-scan base-package="cn.***.springmvc"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 跳转页面目录为WebContent/views/下 -->
<property name="prefix" value="/views/"></property>
<!-- 后缀 跳转页面为jsp文件 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第四步:控制类编写
@Controller
@RequestMapping("/")
public class YgglController {
@Resource(name="ygglService")
private YgglService ygglService;
@RequestMapping(value="login",method=RequestMethod.POST)//,method=RequestMethod.POST
public String hello(String user,String pass) {
System.out.println("login"+user);
System.out.println("login"+ygglService.getConn());
//返回要调用的jsp页面
return "hello";
}
}
jsp
<form action="login" method="post">
账号:<input name="user" />
密码:<input type="password" name="pass" />
<button>登陆</button>
</form>
截止一个简单的用户登陆springMVC的小案例就搭建完成
–菜鸟升阶路上学习记录,欢迎各位大牛批评指正!!