SpringMVC学习笔记二第一个小的程序
首先:我们要用springmvc来写一个helloworld的例子:
首先我们需要导入所需要的架包:
/demo1/WebRoot/WEB-INF/lib/commons-logging-1.1.1.jar
/demo1/WebRoot/WEB-INF/lib/jstl-1.2.jar
/demo1/WebRoot/WEB-INF/lib/spring-aop-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-asm-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-batch-infrastructure-2.2.2.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-beans-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-context-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-context-support-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-core-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-expression-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-jdbc-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-retry-1.0.2.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-test-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-tx-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-web-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-webmvc-3.1.4.RELEASE.jar
第二步:我们要在src这个文件夹下写一个配置的文件,这个文件的名字叫做:springmvc.xml.
代码如下:
<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: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-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<!-- 处理器映射器 根据url匹配bean的name 处理器映射器实现了HandlerMapping接口 -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 简单url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.action">hello_controller</prop>
<prop key="/hello2.action">hello_controller</prop>
<prop key="/hello3.action">hello_controller3</prop>
</props>
</property>
</bean>
<!-- 处理器适配器 实现了HandlerAdapter接口 action按照适配器要求开发 ,规则是实现Controller接口 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 配置HttpRequestHandlerAdapter处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置action -->
<bean id="hello_controller" name="/helloworld.action" class="cn.com.springmvc.Hello" />
<!-- 配置action -->
<bean id = "hello_controller3" class="cn.com.springmvc.Hello1"/>
<!-- 学生信息修改 -->
<bean name="/editstudent.action" class="cn.com.springmvc.student.action.EditStudent"/>
<bean name="/editstudentsubmit.action" class="cn.com.springmvc.student.action.EditStudentSubmit"/>
</beans>
第三步:在web.xml这个文件中配置springmvc所需要的东西:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc1215_1</display-name>
<!-- post乱码处理 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation指定 springmvc的全局配置文件 如果 contextConfigLocation不指定,默认找配置文件名称:servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
第四步:就是写我们所需要的Java代码:
public class Hello implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
//向页面显示一行提示信息
//下边的方法就相当于request.setAttribute(arg0, arg1)
modelAndView.addObject("message", "helloworld!!!!!!");
//指定jsp页面地址
//modelAndView.setViewName("/WEB-INF/jsp/hello.jsp");
//指定逻辑视图名,真正的视图地址:前缀+逻辑视图名+后缀
modelAndView.setViewName("hello");
return modelAndView;
}
}
第五步:就是写我们的界面这个就自己写吧。
SpringMVC学习笔记二第一个小的程序的更多相关文章
- springMVC学习笔记(二)-----注解和非注解入门小程序
最近一直在做一个电商的项目,周末加班,忙的都没有时间更新博客了.终于在上周五上线了,可以轻松几天了.闲话不扯淡了,继续谈谈springMvc的学习. 现在,用到SpringMvc的大部分使用全注解配置 ...
- SpringMVC学习笔记二:常用注解
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831976.html 参考:http://www.cnblogs.com/leskang/p/5445698 ...
- SpringMVC学习笔记(二)
一.HandleMapping和HandlerAdapter的讲解 HandleMapping:处理映射器,可以理解为为请求的url查找对应的Controller类. HandlerAdapter:可 ...
- springmvc学习笔记二:重定向,拦截器,参数绑定
Controller方法返回值 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. 返回void 在Contro ...
- springMVC学习笔记二
六.springmvc的注解 xml的配置→注解 1.新建一个配置文件还是在config下 新建springAnnotation-servlet.xml web.xml 修改初始化为<param ...
- SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解
版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents 1.1. @RequestMapping映射请求 Spring ...
- SpringMVC学习笔记二:参数接受
该项目用来介绍SpringMVC对参数接受的方法: 项目目录树:在前一个项目上修改添加 新添加了Student类和Group类,用来测试整体参数接受 Student.java package com. ...
- Beego 学习笔记二:第一个项目
第一个MVC项目 1> 使用beego命令,创建一个项目 首先切换到创建项目的位置,输入bee new firstweb命令,创建成功之后会出现一个名为firstweb的文件夹 2> ...
- Arduino学习笔记二:修改LED点灯程序
看了开源社区的LED控制程序,开始上手代码编写,修改,下载以及调试,原文地址:http://www.arduino.cn/thread-1072-1-1.html,这个帖子写的比较通俗易懂. 自己移植 ...
随机推荐
- shell 中的$0 $1 $* $@ $# $$ $? $() $(())
$0: 脚本本身文件名称 : 命令行第一个参数,$2为第二个,以此类推 $*: 所有参数列表 $@: 所有参数列表 $#: 参数个数 $$: 脚本运行时的PID $?: 脚本退出码 ∗与@的区别 当命 ...
- C语言中scanf函数的实现
接上一篇C语言中可变参数函数实现原理,从理论上详细介绍了C语言中可变参数函数的实现,这一篇从minix内核源码中的scanf函数入手,学习C语言经典可变参数函数的实现过程 在scanf.c文件中,可以 ...
- Laya 图集动画
参考: 图集动画运用 一.准备素材 从爱给网找到几个素材 二.使用Laya的图集工具 菜单栏选择工具->图集打包工具,然后选择序列图所在的文件夹 生成了个.rec...说好的.atlas呢... ...
- matlab中 数据保留有效位数
可以使用round函数 ,这函数原本功能是四舍五入 比如: >> A = 0.0326465;>> B = round(A*1000)/1000 B = 0.0330
- Logstash在Linux上安装部署
Logstash 简介: Logstash 是一个实时数据收集引擎,可收集各类型数据并对其进行分析,过滤和归纳.按照自己条件分析过滤出符合数据导入到可视化界面.它可以实现多样化的数据源数据全量或增量传 ...
- [分布式系统学习]阅读笔记 Distributed systems for fun and profit 之四 Replication 拷贝
阅读http://book.mixu.net/distsys/replication.html的笔记,是本系列的第四章 拷贝其实是一组通信问题,为一些子问题,例如选举,失灵检测,一致性和原子广播提供了 ...
- ABP之仓储
一.仓储的简单介绍 仓储(Repository):这是属于领域层的重要组成部分,它的作用就是完成和数据库的交互工作,仓储里封装了很多操作数据库的方法.所以说仓储是数据映射层和领域层的交互中介.ABP针 ...
- IIS 下载文件 报错“401 - 未授权: 由于凭据无效,访问被拒绝。”
点开身份验证 改为启用就OK了 重启一下IIS. 如果你上在办法没有解决可参考 1.打开“IIS信息服务管理器”——>选择你发布的网站——>选择功能视图中的“身份验证”——>右键匿名 ...
- hdu3974 Assign the task【线段树】
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in
地址:http://stackoverflow.com/questions/18852983/eclipse-reports-rendering-library-more-recent-than-ad ...