springmvc使用和经验总结(长沙师说网络科技有限公司)
springmvc
先分析下代码,高速学习。先要把配置文件写好,
给上2个类详细看看
package com.shishuo.studio.action; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.shishuo.studio.constant.SystemConstant;
import com.shishuo.studio.entity.Category;
import com.shishuo.studio.entity.vo.CourseVo;
import com.shishuo.studio.entity.vo.PageVo;
import com.shishuo.studio.exception.CategoryNotFoundException;
import com.shishuo.studio.exception.notfound.StorageNotFoundException;
import com.shishuo.studio.service.CategoryService;
import com.shishuo.studio.service.UserService; /**
* @author Herbert
*
*/
@Controller
@RequestMapping("/category")
public class CategoryAction extends BaseAction { protected final Logger logger = Logger.getLogger(this.getClass()); @Autowired
protected CategoryService categoryService; @Autowired
protected UserService userService; /**
* 首页
*
* @param modelMap
* @return
*/
@RequestMapping(value = "/{categoryId}.htm", method = RequestMethod.GET)
public String category(@PathVariable long categoryId, ModelMap modelMap,
@RequestParam(value = "p", defaultValue = "1") int p) {
try {
// 获得数据
Category category = categoryService.getCategoryById(categoryId);
// 获取当前文件夹下的全部课程
PageVo<CourseVo> coursePageVo = courseService
.getCoursePageByIdForUser(categoryId, p, 24);
// 添加属性
modelMap.addAttribute("category", category);
modelMap.put("coursePageVo", coursePageVo);
return "category";
} catch (CategoryNotFoundException e) {
return SystemConstant.PAGE_404;
} catch (StorageNotFoundException e) {
// TODO Auto-generated catch block
return SystemConstant.PAGE_404;
} }
}
package com.shishuo.studio.action; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.shishuo.studio.action.auth.AuthBaseAction; @Controller
@RequestMapping("/about")
public class AboutAction extends AuthBaseAction { /**
* 跳转到关于我们页面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/about.htm", method = RequestMethod.GET)
public String about(ModelMap modelMap, HttpServletRequest request) {
return "/about/about";
} /**
* 跳转到服务协议页面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/service.htm", method = RequestMethod.GET)
public String service(ModelMap modelMap, HttpServletRequest request) {
return "/about/service";
} /**
* 跳转到投诉举报页面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/complain.htm", method = RequestMethod.GET)
public String complain(ModelMap modelMap, HttpServletRequest request) {
return "/about/complain";
} /**
* 跳转到版权声明页面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/copyright.htm", method = RequestMethod.GET)
public String copyright(ModelMap modelMap, HttpServletRequest request) {
return "/about/copyright";
} /**
* 跳转到联系我们页面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/connect.htm", method = RequestMethod.GET)
public String connect(ModelMap modelMap, HttpServletRequest request) {
return "/about/connect";
} }
return "system/comment/comment";后面不须要东西
return "redirect:/admin/comment/page.htm";一般当我改变一个状态的时候 我须要还是显示在当前页面 就须要再进入Action 相当于再到数据库訪问一次把 我改变的数据同个pageVo 显示到页面
spring的注解学习
@RequestParam("description") String description,
@PathVariable
请求路径上有个id的变量值,能够通过@PathVariable来获取 @RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
@autowired 自己主动配置 不须要写getter() setter()方法
@Deprecated 过时
@Repository 用在接口前面的类 比方ibits接口类的最前面
@ResponseBody当控制器返回页面不是字符串的时候 比方返回一个json对象用这个注解
@Controller控制器 加在控制器类的最前面
@RequestMapping("/admin/file")
放在类前面是这个路径下
@RequestMapping(value = "/index.htm", method = RequestMethod.GET)假设这个注解放在方法的前面 表示上面那个路径的基础下然后再是这个路劲
@RequestParam(value = "fileId", defaultValue = "1")当url传入參数的时候就能够拿到值
比方@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
public String update(
@RequestParam(value = "fileId", defaultValue = "1") long fileId,
ModelMap modelMap) throws Exception {}
复制spring相关jar包到web-inf/lib里面
<servlet>
<servlet-name>spring</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>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
然后在application.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 自己主动扫描的包名 -->
<context:component-scan base-package="com.shishuo.studio"></context:component-scan> <mvc:annotation-driven /> <task:annotation-driven />
<tx:annotation-driven /> <bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.shishuo.studio.filter.GlobalInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/auth/**" />
<bean class="com.shishuo.studio.filter.AuthInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/auth/studio/**" />
<bean class="com.shishuo.studio.filter.StudioInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 在XML配置文件里增加外部属性文件,当然也能够指定外部文件的编码 -->
<bean id="propertyConfigurer" class="com.shishuo.studio.util.PropertyUtils">
<property name="locations">
<list>
<value>classpath:shishuo.studio.properties</value> <!-- 指定外部文件的编码 -->
</list>
</property>
</bean>
<!-- FreeMarker的配置 -->
<bean id="freeMarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl" /><!--
指定路径 -->
<property name="defaultEncoding" value="UTF-8" /><!-- 指定编码格式 -->
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
</bean>
<!-- 配置 FreeMarker视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="cache" value="false" />
<property name="prefix" value="/" />
<property name="suffix" value=".ftl" /><!--可为空,方便实现自已的根据扩展名来选择视图解释类的逻辑 -->
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
</beans>
@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
它的作用和在xml文件里使用bean节点配置组件时一样的。
@Component Component是Spring管理组件的通用形式,而@repository,@Service,@Controller是它的细化。分别表示更加详细的用例(分别相应持久化层,服务层和表现层)
B、依照Class路径扫描
XML风格的配置方式。我们会在配置文件里配置大量的bean。这样但项目足够大时,那么这个配置文件将过于庞大而不便管理。而应用@凝视的配置方式。我们在类中用@Component等凝视类,并让容器依照Classpath自己主动扫描管理它们。要实现以上功能我们须要这样定义。
<?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:context="http://www.springframework.org/schema/context"
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="org.example" />
</beans>
在使用组件扫描时,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor也将隐式包括进来,也就是说它支持@Autowired等,不须要我们如用<context:annotation-config/>再做声明了。。
自己主动扫描包名的配置 <context:component-scan base-package="com.shishuo"></context>
当我们用spring mvc 前端控制器的时候须要配置
<!-- spring mvc 基于注解在方法上 控制映射 配置 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- 在XML配置文件里增加外部属性文件。当然也能够指定外部文件的编码 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:shishuocms.properties</value> <!-- 指定外部文件的编码 -->
</list>
</property>
</bean>
<!-- 配置 FreeMarker视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="cache" value="false" />
<property name="prefix" value="/" />
<property name="suffix" value=".ftl" /><!--可为空,方便实现自已的根据扩展名来选择视图解释类的逻辑 -->
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
<!--创建数据映射器。数据映射器必须为接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="com.shishuo.cms.dao" />
</bean>
spring mvc 拦截器
拦截器在application.xml配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.shishuo.cms.filter.GlobalInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
怎么使用RequestMapping的參数 主要有RequestParam Pathvariable(这个注解是获取url里面的參数)
5.1.1、常见应用场景
1、日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。
2、权限检查:如登录检測。进入处理器检測检測是否登录,假设没有直接返回到登录页面。
3、性能监控:有时候系统在某段时间莫名其妙的慢。能够通过拦截器在进入处理器之前记录開始时间。在处理完后记录结束时间。从而得到该请求的处理时间(假设有反向代理。如apache能够自己主动记录);
4、通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便兴许流程使用,还有如提取Locale、Theme信息等,仅仅要是多个处理器都须要的就可以使用拦截器实现。
5、OpenSessionInView:如Hibernate,在进入处理器打开Session,在完毕后关闭Session。
…………本质也是AOP(面向切面编程),也就是说符合横切关注点的全部功能都能够放入拦截器实现。
MySQL server version for the right syntax to use near where userId=28 at line 1
这个错误是在写改动语句的时候 where 前面多加了一个逗号
syntax error 是语法错误
Could not resolve view with name 'auth/teacher/skill/update' in servlet with是自己没有加@Responsebody 用spring mvc 的时候返回是json 一定要记得写@Responsebody
当须要上传一个form里面包括 文件 或者视屏的时候 一定要记得在form表单后面加入 enctype="multipart/form-data" enctype="multipart/form-data"
@RequestParam @RequestBody @PathVariable 等參数绑定注解具体解释
分类: spring 2012-09-21 16:22 11494人阅读 评论(4) 收藏 举报
文件夹(?
)[+]
引言:
接上一篇文章。对@RequestMapping进行地址映射解说之后。该篇主要解说request 数据到handler method 參数数据的绑定所用到的注解和什么情形下使用;
简单介绍:
handler method 參数绑定经常使用的注解,我们依据他们处理的Request的不同内容部分分为四类:(主要解说经常使用类型)
A、处理requet uri 部分(这里指uri template中variable,不含queryString部分)的注解: @PathVariable;
B、处理request header部分的注解: @RequestHeader, @CookieValue;
C、处理request body部分的注解:@RequestParam, @RequestBody;
D、处理attribute类型是注解: @SessionAttributes, @ModelAttribute;
springmvc使用和经验总结(长沙师说网络科技有限公司)的更多相关文章
- 云计算:创业的好时机——上海够快网络科技有限公司总经理蒋烁淼专访(评价阿里云的OSS的4个优点)(够快科技正式宣布已成功挂牌新三板)
云存储是云计算目前的热点之一,Dropbox.Box等产品的风靡,公司因此获得极高估值,都印证了这一点.但云存储对技术和资金要求都比较高,竞争也非常激烈,挑战巨大.国外云存储公司有亚马逊的云平台作为支 ...
- 龙六网络科技有限公司(Dragon six Network Technology Co., Ltd.)
龙六网络科技有限公司(Dragon six Network Technology Co., Ltd.)
- 海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs
海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs 企业简介 武汉海蜘蛛网络科技有限公司成立于2005年,是一家专注于网络新技术研 ...
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)【申明:来源于网络】
SSM框架--详细整合教程(Spring+SpringMVC+MyBatis)[申明:来源于网络] 地址:http://blog.csdn.net/u014662268/article/details ...
- ZOJ Goldbach 2013年长沙赛区网络赛
迟到了一天的AC.... 思路: 先把单个素数 或着 两个素数能组成的情况预处理一下,然后对于给出的 n,拿第三个素数去和两个素数的情况匹配,最后要注意去重. 详情见代码. 因为手残少敲了一个 els ...
- 双硬盘Win7装Ubuntu 12.04经验并解决无线网络不能使用问题
RFKill Many computer systems contain radio transmitters, including Wi-Fi, Bluetooth, and 3G devices. ...
- 1-趣味解读DNS工作原理——转载疯猫网络科技
因为只要我们输入百度.腾讯.淘宝的名字,无论它们的服务器在哪里,历经多少轮查询,我们都能找到并访问之.这就是计算机网络中著名的域名系统DNS(Domain Name System),它能实现把一个网站 ...
- html阿里云网页练习实现代码
html <body> <!-- 固定浮动栏 --> <div class="guding"> <p> ...
- iOS:根据日志去定位网络请求发生的错误是由于服务端造成的,还是客户端造成的?
一.介绍 在项目开发中,服务端和客户端的协作尤为重要,而连接它们的最重要的环节之一就是网络请求,对于服务端而言,如果这个环节出现了错误,那么安全性就无从谈起,同时对于客户端而言,如果这个模块出现了错误 ...
随机推荐
- spring+mybatis+Atomikos JTA事务配置说明
一.概览 Atomikos是一个公司名字,旗下最著名的莫过于其Atomikos的事务管理器产品.产品分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTrans ...
- Accelerated C++:通过演示样例进行编程实践——练习解答(第9章)
我的Github地址:https://github.com/lanbeilyj/Accerlerated-C-plus-plus 9-0. Compile, execute, and test the ...
- 一筐梨子&一筐水果——协变性(covariant)
假设突然看见这个问题.我们常常会想当然. 一个梨子是水果,一筐梨子是一筐水果吗? watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXFqMjA2NQ==/f ...
- Linux下常用的中文输入法平台有IBus、fcitx和scim
Linux下常用的中文输入法平台有IBus.fcitx和scim.scim现在维护滞后,不推荐使用. IBus ("Intelligent Input Bus") 是一个 输入法框 ...
- 1.1 Introduction中 Kafka as a Messaging System官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Messaging System kafka作为一个消息系统 ...
- ASP.NET路径解惑
对于ASP.NET的路径问题,一直都是云里雾里,没有去详细的理解,今天正好可以梳理一下它们之间的关系和使用方法.而若想明白路径的表示方式的使用方法和区别以及注意事项可以通过下面的几个概念来进一步加深: ...
- 学习笔记:Vue——自定义指令
在Vue2.0中,代码复用和抽象的主要形式是组件.然鹅,有的情况下,你仍然需要对普通DOM元素进行底层操作,这时候就会用到自定义指令. 1.举个聚焦输入框的例子,全局注册focus指令 Vue.dir ...
- Flask项目之手机端租房网站的实战开发(十二)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛 hdu 6150】Vertex Cover
[链接]点击打开链接 [题意] 有人写了一个最小点覆盖的贪心算法,然后,让你去hack它. 并且,要求这个算法得到的错误答案,是正确答案的三倍. 让你任意输出hack数据,点数<=500 [题解 ...
- 硬件——nrf51822第三篇,按键控制小灯
现象是按键按下,小灯亮,按键抬起,小灯灭. 从这一节我们细致剖析gpio口的设置: nrf51822片上一共有32个数字引脚,分为4个port,如下: port 0 pin 0-7 port 1 pi ...