首先导入所需的jar包,项目目录结构如下:

之后需要配置一下web.xml文件,内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

然后配置applicationContext.xml:

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.sprmvc.po"></context:component-scan>
<!-- 配置视图解析器 将HelloWorldController中的返回值解析为实际的物理视图 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix为前缀,suffix为后缀 -->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

下面开始建立实体类User.java:

 package com.sprmvc.po;

 public class User {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

然后是控制层代码:

 package com.sprmvc.po;

 import javax.servlet.http.HttpServletRequest;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HelloWorldController {
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String printHelloWorld(HttpServletRequest request,User user) {
request.setAttribute("userName", user.getUserName());
request.setAttribute("password", user.getPassword());
return "hello";
}
}

这里的value即访问路径,而return的"hello"通过applicationContext.xml中配置的视图解析器会返回到hello.jsp中

接下来我们建立两个.jsp页面,首先是index.jsp:

 <body>
<form method="get" action="hello">
用户名:<input type="text" name="userName">
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>

这里的method和action分别与控制层中的method和value的值相对应,即HelloWorldController.java中的第11行。

然后是hello.jsp:

 <body>
<h1>操作成功了</h1>
您的用户名为:${userName}<br>
您的密码为:${password }
</body>

这样只需要将项目加载到tomcat下就可以进行访问了,赶快试试把。

SpringMVC的简单示例的更多相关文章

  1. SpringMVC + MyBatis简单示例

    该项目基于Maven开发,该项目中包含了MyBatis自动创建表的功能,具体实现查阅MyBatis---自动创建表 源码下载 配置 maven支持pom.xml <project xmlns=& ...

  2. SpringMVC之简单的增删改查示例(SSM整合)

    本篇文章主要介绍了SpringMVC之简单的增删改查示例(SSM整合),这个例子是基于SpringMVC+Spring+Mybatis实现的.有兴趣的可以了解一下. 虽然已经在做关于SpringMVC ...

  3. springMVC源码分析--异常处理机制HandlerExceptionResolver简单示例(一)

    springMVC对Controller执行过程中出现的异常提供了统一的处理机制,其实这种处理机制也简单,只要抛出的异常在DispatcherServlet中都会进行捕获,这样就可以统一的对异常进行处 ...

  4. springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目

    一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...

  5. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  6. springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置

    Log4j由三个重要的组件构成: 日志信息的优先级 日志信息的输出目的地 日志信息的输出格式 日志信息的优先级从高到低有ERROR.WARN. INFO.DEBUG,分别用来指定这条日志信息的重要程度 ...

  7. springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试

    包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...

  8. springmvc 项目完整示例03 小结

    利用spring 创建一个web项目 大致原理 利用spring的ioc 原理,例子中也就是体现在了配置文件中 设置了自动扫描注解 配置了数据库信息等 一般一个项目,主要有domain,dao,ser ...

  9. springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用

    百度百科: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBat ...

随机推荐

  1. mvc bundle功能(1)

    现如今都提倡敏捷开发,快速开发,但是再要求速度的同时,还得保证质量!前端我是没办法,毕竟是直接要面向用户的,但是后台,解决方案那就多了,诸如extjs,bootstrap,kendoui,都可以解决. ...

  2. 如何在安裝SELinux的环境执行Quartus II

    (原創) 如何在安裝SELinux的環境執行Quartus II? (SOC) (Quartus II) (Linux) (RedHat) Abstract一般人安裝Linux時,也會同時安裝SELi ...

  3. HDU-3864 D_num Miller_Rabin和Pollard_rho

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...

  4. JAVA中“==”与equals()方法区别

    equals 方法是 java.lang.Object 类的方法 有两种用法说明: ()对于字符串变量来说,使用"=="和"equals()"方法比较字符串时, ...

  5. C:移位运算符

    1在向右移位时,空出的位是由0填充,还是由符号位的副本填充? 如果被移位的对象是无符号数,那么空出的位将被0填充.如果被位移的对象是有符号数,那么C语言实现既可以用0填充空出的位,也可以用符号位的副本 ...

  6. jquery easyui中的dialog拖动超出浏览器问题解决办法

    juqery easyui当鼠标点着拖动超出浏览器后,就回不来了,拉不下的问题: //控制dialog超出浏览器回到原来的地方 var default_left; var default_top;// ...

  7. codeforces 652B z-sort(思维)

    B. z-sort time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  8. LoadRunner显示中文乱码的问题

    lr_convert_string_encoding Converts a string to a different encoding. Return Values:This function re ...

  9. 在没备份undo的情况下,undo丢失,重启数据库报ORA-01157错误

    今天做了一下undo隐藏参数的实验 在没有备份的情况下,删除正在使用的undo,然后关机 (本次使用的的oracle的隐藏参数,慎用!!!!!!!!!!!!!!) idle> select * ...

  10. opencv 模板匹配与滑动窗口(单匹配) (多匹配)

    1单匹配: 测试图片:   code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <open ...