在eclipse上搭建SpringMVC环境
来源:互联网 发布:windows 2012 r2 编辑:程序博客网 时间:2024/06/05 22:57
这几天学习了SpringMVC,在这里写一下如何在eclipse上搭建SpringMVC环境。
1.首先导入SpringMVC所需jar包:http://repo.spring.io/release/org/springframework/spring/(具体可以在上述网站下载所需版本)。将下载好的jar包导入progect的WEB-INF下的lib文件夹下
2.目录结构
3.配置文件
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<display-name>webTest</display-name>
<servlet>
<servlet-name>webTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webTest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
其中servlet下的<param-value>/WEB-INF/config/springmvc.xml</param-value>为对Springmvc的config问价的配置。如果springmvc.xml文件在其他问价夹下则该路径也要做出相应的改变。
(2)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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.springMVC" /><!-- 配置读取 springMVC扫描目标包中的类 -->
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/"/><!-- 配置css文件目录 -->
<mvc:resources mapping="/*.html" location="/"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置视图解析器 -->
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.UserController类
package com.springMVC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/doUserLogin")
public String douserLogin(){
return "userLogin";
}
@RequestMapping(value="/userLogin")
public String userLogin(String username, String password){
if(userService.checkUser(username,password)){
return "loginSuccess";
}
return "loginFailure";
}
}
5.UserService类
package com.springMVC;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Resource
private UserDao userDao;
public Boolean checkUser(String username, String password){
return userDao.checkUser(username,password);
}
}
6.userDao类
package com.springMVC;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
//连接数据库的部分在这里不做介绍,仅仅介绍实现SpringMVC的运行
public Boolean checkUser(String username, String password){
if("zhangsan".equals(username) && "123456".equals(password)){
return true;
}
else{
return false;
}
}
}
7.jsp文件
(1)userLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>userLogin</title>
</head>
<body>
<div>
<form action="userLogin" method="post">
<tr>
<td>username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td rowspan="2"><input type="submit" value="登录"/></td>
</tr>
</form>
</div>
</body>
</html>
(2)sucess
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>success</title>
</head>
<body>
登录成功!
</body>
</html>
(3)failure
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>failure</title>
</head>
<body>
用户名或密码错误!
</body>
</html>
上述过程只涉及到了基本框架的实现,做得比较粗糙。在浏览器上输入localhost:8080/webTest/doUserLogin 回车即可跳转到登录界面。
- 在eclipse上搭建SpringMVC环境
- Alfresco在eclipse上debug环境搭建
- 在Eclipse上搭建Android 2.3环境
- 在eclipse上搭建drools开发环境
- 在Eclipse上搭建Android开发环境
- 如何在Eclipse上搭建python环境
- 在eclipse上搭建Ionic环境
- 如何在eclipse上搭建servlet环境
- 在Eclipse上搭建Nokia手机的J2ME开发环境
- 在Eclipse上搭建JAVA可视化开发环境
- window下在Eclipse上搭建perl开发环境
- 搭建在Eclipse上开发Heroku应用的环境
- 在Eclipse上搭建JAVA可视化开发环境
- 在Eclipse上搭建JAVA可视化开发环境
- cocos2d-x在eclipse上搭建开发环境
- cocos2d-x在eclipse上搭建开发环境…
- 在windows上用eclipse搭建STM32开发环境
- 在Netbeans和Eclipse上搭建Android开发环境教程
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- 什么是面向对象
- solrlin代码质量检查问题总结
- github基本使用
- 统计不同单词个数
- 在eclipse上搭建SpringMVC环境
- python的 局部变量和全局变量
- 动态稀疏矩阵的两个三元组进行相加的算法
- SELECT查询命令的使用
- Java+opencv3.2.0之环境配置
- 软件测试之使用jmeter进行压力测试
- 2.3 Spring的核心机制:依赖注入
- 串口通讯demo-serial port communication 供参考
- 十四、流程控制语句