SpringMVC注释启用
这篇文章是我学习的网络视频SpringMVC写的过程。
谢谢公布各位前辈的视频
以下评论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>springMVC1</display-name>
<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>
<!-- springMVC 入口 dispatcher -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 载入配置文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
</init-param>
<!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
其次,配置文件。最基本的是开启注解和Spring启动时载入扫描包
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- spring 启动时扫描包 -->
<context:component-scan base-package="com.tgb.web.controller.annotation">
</context:component-scan> <!-- 开启注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> <!-- 路径相应调用的Controller -->
<bean name="/test/helloworld" class="com.tgb.web.controller.HelloWorldController"/>
<!-- 视图解析 -->
<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> <!-- 静态资源訪问(不拦截此文件夹下的东西的訪问) -->
<mvc:resources location="/img/" mapping="/img/**" /> </beans>
再次,书写Controller的java代码
package com.tgb.web.controller.annotation; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController { @RequestMapping(value="/user/addUser",method=RequestMethod.POST)
public ModelAndView addUser(){
String result = "addUser";
return new ModelAndView("/annotation","result",result);
} @RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser(){
String result = "delUser";
return new ModelAndView("/annotation","result",result);
} @RequestMapping(value="/user/toUser",method=RequestMethod.GET)
public ModelAndView toUser(){
String result = "toUser";
return new ModelAndView("/touser","result",result);
}
}
此处请注意,@Controller的使用。
@RequestMapping(value="/user/addUser",method=RequestMethod.POST)中的value表示跳转路径,method表示通过哪种方式调用这种方法
最后是前台的jsp界面
annotation.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h1>SpringMVC注解</h1>
<br>
${result }
</body>
</html>
touser.jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="/springMVC4/user/addUser" method="post">
<h1>SpringMVC注解</h1>
<br>
${result }
<input type="submit" value="post请求">
</form>
</body>
</html>
SpringMVC注释启用的更多相关文章
- springMVC注解启用及优化
使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用注解方式. Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件 Web.xml文件中不 ...
- Springmvc注解启用
http://www.blogbus.com/wanping-logs/235898637.html 使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用 ...
- 6.SpringMVC注解启用
SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率. 由于 有相当一部分人讨厌xml配置方式 注解可以覆盖 xml则不能 使用注解比xml规范化,因为很多注解都是java的规范的范 ...
- springmvc注释
//请求json串(商品信息) 输出json(商品信息) //@RequestBody将请求的商品信息的json串转成itemsCustom对象 //@ResponseBody将itemsCus ...
- SpringMVC 注释@PathVariable
@PathVariable 是用来获得请求url中的动态参数的: @ResponseBody @RequestMapping(value="/pointUpload/{userid}&quo ...
- IDEA下使用maven构建web项目(SpringMVC+Mybatis整合)
需求背景:由于最近总是接到一些需求,需要配合前端团队快速建设移动端UI应用或web应用及后台业务逻辑支撑的需求,若每次都复用之前复杂业务应用的项目代码,总会携带很多暂时不会用到的功能或组件,这样的初始 ...
- 【转载】SpringMVC配置文件详解
转自:https://my.oschina.net/happyBKs/blog/691502 web.xml文件是web应用的部署描述. 在上一节的springMVC示例中 ,idea下的Maven- ...
- [Java] Spring + SpringMVC + Maven + JUnit 搭建
示例项目下载: https://github.com/yangyxd/SpringDemo 利用前面 SpringMVC 项目的配置方式,完成初步的项目创建.下面只讲一些不同之处. 传送门: [Jav ...
- SpringMVC和MyBatis整合
目前主流的Web MVC框架,除了Struts这个主力 外,还有Spring MVC,主要是由于Spring MVC配置比较简单,使用起来也十分明了,非常灵活,与Spring 集成较好,对RESTfu ...
随机推荐
- RedGate 工具SQLMultiScript1.1
原文:RedGate 工具SQLMultiScript1.1 RedGate 工具SQLMultiScript1.1 SQLMultiScript是一个脚本分发工具,当你写好了一个SQL脚本之后,你需 ...
- 初步C++类模板学习笔记
类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型. -->抽象的类. 在调用类模板时, 指定參数, 由编 ...
- SQL Server 性能调优培训引言
原文:SQL Server 性能调优培训引言 大家好,这是我在博客园写的第一篇博文,之所以要开这个博客,是我对MS SQL技术学习的一个兴趣记录. 作为计算机专业毕业的人,自己对技术的掌握总是觉得很肤 ...
- 【剑指offer】打印单列表从尾部到头部
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25028525 剑指offer上的第五题,在九度OJ上測试通过. 时间限制:1 秒 内存限制 ...
- Ajax的get和post两种请求方式区别
Ajax的get和post两种请求方式区别 (摘录):http://ip-10000.blog.sohu.com/114437748.html 解get和post的区别. 1. get是把参数数据队列 ...
- eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge found
eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge fou ...
- 用友财务总帐(GL)模BI数据ETL分析
业务需求,如下面的: 现在用友总帐一家公司BI分析案例. /* Sql Server2012使用作业设置定时任务,为了保证有一天运行时间 */ /* 意temp1表里一定要保证要有记录,否则以temp ...
- 学习pthreads,创建和终止多线程
更CPU多线程编程,通过笔者的研究发现,,pthreads使用日趋广泛.它是螺纹POSIX标准,它定义了一组线程的创建和操作API. 配置环境见上博客文章.配置环境后,只需要加入#include &l ...
- Java 解析 lnk 快捷方式文件的方法(转)
package file.extendsion; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.F ...
- UVA - 11637 Garbage Remembering Exam (组合+可能性)
Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear ...