在eclipse上搭建SpringMVC环境

这几天学习了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 回车即可跳转到登录界面。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值