首先导入所需的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. pyinstaller使用小结

    安装pyinstaller pip install -U pyinstaller 生成控制台程序 pyinstaller ./example.py 在当前目录的dist文件夹内可以找到编译成功的程序 ...

  2. Using FastCGI to Host PHP Applications on IIS 7 -IIS7 怎么配置 PHP5

    This article describes how to configure the FastCGI module and PHP to host PHP applications on IIS 7 ...

  3. 生成chm文档工具- Sandcastle -摘自网络

    Sandcastle是微软官方的文档生成工具,NDoc开发停止后,这个貌似也是唯一的一个这方面的工具.它从dll文件及其xml注释文件能够 生成完整的帮助文档,支持多种生成格式(Helpe1x:chm ...

  4. Linux之一次性安装开发工具:yum groupinstall Development tools

    [spark@sparksinglenode ~]$ yum grouplist | moreLoaded plugins: fastestmirror, refresh-packagekit, se ...

  5. Apache Spark GraphX的简介

    简单地说,GraphX是大规模图计算框架. GraphX 是 Spark 中的一个重要子项目,它利用 Spark 作为计算引擎,实现了大规模图计算的功能,并提供了类似 Pregel 的编程接口. Gr ...

  6. homework-08 C++课程课后思考与练习

    经过上次晚交作业导致没分以后 我再也不敢晚交作业了 今天就把这次作业先写了 homework Part 1 1. 理解C++变量的作用域和生命周期 a) 用少于10行代码演示你对局部变量的生命周期的理 ...

  7. HDU 5787 K-wolf Number (数位DP)

    K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...

  8. 常用的Activex 控件

    1. Flash Player  ActiveX Control 6.0.47.0 与FLASH 6.0配套的浏览器端动画播放插件                  download.pchome.n ...

  9. MySQL安装配置最后时未响应解决方法

    安装MySQL出示未响应,一般显示在安装MySQL程序最后一步的2,3项就不动了. 这种情况一般是你以前安装过MySQL数据库服务项被占用了.解决方法:一种方法:你可以安装MySQL的时候在这一步时它 ...

  10. 转载C# 对象转Json序列化

    转载原地址:  http://www.cnblogs.com/plokmju/p/ObjectByJson.html JSON Json(JavaScript Object Notation) 是一种 ...