About spring MVC
can we modify form bean?--yes.when you want to use it in page, be like this: Object["{attribute name}"]
can we modify model?--yes, if you add a new attribute with same property name, the old one will be replaced.
example:lets say we want to keep some content when we have to jump to another page:
  1. we use model.addAttribute to set each attribute in model. (can also through formbean, while then you can not use ${attributeName} directly, have to use formbean["attributeName"]).
  2. in the page we jumped to, we use <springform:hidden "path"="xxx" value="${xxx}"/> to make the properties stay on page(unvisible)
  3. we also make sure that those attributes defined in the pojo bean related with the from, so when the form was commitd from second page, we got the attributes from the object.
pay attention1:
if you used a 
<spring:url value="${path}" var="form_url">
        <spring:param name="twitle" value="${twitterTitle}"/>
</spring:url>
should avoid using twitle as property of form bean, otherwise, in form bean, this property will be mess like "{perperty value},{mapping string value}"
pay attention2:
            you can use same property name in model and form bean, they will not affect each other, once I used secretQuestion in both form bean and model property, to save question code and question lable, so I save both of then don't need to transfer to eachother.
--------------
form use post as method by default, spring mvc controller mathod use get as method by default, that why the form action string not lead the request  to the method sometimes.

notes:
the <spring:message code="" param=""> the param should not contain ','. (other punctuation not tested)!!! otherwise the content after the puntuation will be lost!
----------------------
Can not jump to a pulic page from an securated url method, example: mathod a response for url"/resendTokenEmailForm", in the end of this method, should not return "public/resendTokenEmailForm", because it's a unsecurated page, even you call logout mehtod fist. the right way is to call 'return "redirect:/public/resendTokenEmailForm"', to go to the controller method first, then in that method, return "public/resendTokenEmailForm". 
when doing this, you can use model.addattribute() to add parameter or user "?emailAddress="+tEmail+"&secretQuestion="+tQes" to transfer the parameters.
what if you do modal.addattribute("email", "abc") and add "?email=abc" at the end of url together? the model.get ("email") will be "abc,abc"
-----------------------------
How to change the mothed of form from default "POST" to "PUT"?
when we use a form to do update, we used the default path like "/mainorders" this is the same path with list and create method. So if we still use "method = RequestMethod.POST" in method mapping annotation, it will duplicated with the create method, if don't set, by default it's a GET, so it a conflict with the list method..... then what we should do is to set it to "method = RequestMethod.PUT". here is how to write in jsp file.
<input type="hidden" value="PUT" name="_method"/>
-----------------------------------------------
窗口要用Post参数才能传递
that we must use Post to make sure the parameter defined in URLString can be delievered here. if we use GET, then doesn't work.
      and the & in URLString must be &amp;
如何封装多个窗口参数
use <form modelAttribute=”conditions”> in View, And use ( FilterBean conditions作为Controller里方法的参数,整个窗体里的内容就被尽量封装进一个FilterBean实例里传给控制器

-- Sam 08:57 11/07/2014

 Spring @t'ransactional annotation doesn't work resons:
  1. is called from an method in the same class, which is not annotated as trsactional.
  2. throught out an exception which was not included in the exception list wich will trigger the roolback event.
-- Sam 11:56 17/11/2019
Spring 7大模块:
  1. Spring Core (IOC, ID)
  2. Spring Context  (i18n-- MessageSource, current user---userContextService)
  3. Spring DAO 
  4. Spring ORM
  5. Spring AOP
  6. Spring Web
  7. Spring WebMVC\
Spring 获取数据库连接池的四种方式
  1. DBCP 数据源
  2. C3P0数据源
  3. Spring数据源
  4. JNDI数据源
Spring3.0基于Annotation的依赖注入的实现
  1. 使用@repository、@Service、@Controller、@Component将类标示为bean。
  2. 使用 @PostConstruct 和 @PreDestroy 指定生命周期回调方法
  3. 使用 @Required 进行 Bean 的依赖检查
    1. @Required 注解只能标注在 Setter 方法之上。
    2. 为了让 Spring 能够处理该注解,需要激活相应的 Bean 后处理器。要激活该后处理器,只需在 XML 中增加如下一行即可。
       <context:annotation-config/>
  4. 用 @Resource、@Autowired 和 @Qualifier 指定 Bean 的自动装配策略
    1. no -- 显式指定不使用自动装配。
    2. byName -- 如果存在一个和当前属性名字一致的 Bean,则使用该 Bean 进行注入。如果名称匹配但是类型不匹配,则抛出异常。如果没有匹配的类型,则什么也不做。
    3. byType -- 如果存在一个和当前属性类型一致的 Bean ( 相同类型或者子类型 ),则使用该 Bean 进行注入。byType 能够识别工厂方法,即能够识别 factory-method 的返回类型。如果存在多个类型一致的 Bean,则抛出异常。如果没有匹配的类型,则什么也不做。
    4. constructor -- 与 byType 类似,只不过它是针对构造函数注入而言的。如果当前没有与构造函数的参数类型匹配的 Bean,则抛出异常。使用该种装配模式时,优先匹配参数最多的构造函数。
    5. autodetect -- 根据 Bean 的自省机制决定采用 byType 还是 constructor 进行自动装配。如果 Bean 提供了默认的构造函数,则采用 byType;否则采用 constructor 进行自动装配。
  5. AOP三种植入切面的方法:
    1. 其一是编译期织入,这要求使用特殊的Java编译器,AspectJ是其中的代表者;
    2. 其二是类装载期织入,而这要求使用特殊的类装载器,AspectJ和AspectWerkz是其中的代表者;
    3. 其三为动态代理织入,在运行期为目标类添加增强生成子类的方式,Spring AOP采用动态代理织入切面。
      1. Spring AOP使用了两种代理机制,之所以需要两种代理机制,很大程度上是因为JDK本身只提供基于接口的代理,不支持类的代理。
        1. 一种是基于JDK的动态代理,
        2. 另一种是基于CGLib的动态代理
-- Sam 08:01 28/08/2014
jsp 页面语法错误导致的貌似离奇的错误:
java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.views.public_.list_005fmore_005fblog_jspx
-- Sam 04:39 20/07/2014
spingmvc中 MessageSource的使用
可以在controller中使用 @Inject private MessageSource messageSource;然后用return messageSource.getMessage("SHOW_TO_EVERY_ONE", null, locale);来从资源文件获得字符串。
可是,如果中间进行了Controller跳转,要注意,除了当前的Controller以外的Controller中的@Inject private MessageSource messageSource;可能并没有被赋予正确的实例,从而导致最终不能取出字符串,例如:RemarkController中响应了create remark事件,但是调用了publicController的showDetailTwitter方法显示结果。
目前做法是如果一定要跳转(指调用其他Controller的方法,则避免用到其他Controller中注入的messageSource。
-- Sam 07:42 14/07/2014
 SpringMVC 中默认支持Theme,如果需要修改
  1. webmvc-config.xml里定义themeResolver的实现类。
        <bean class="com.aeiou.bigbang.util.MyCookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="1"/>
  2. 实现类中:  MyCookieThemeResolver extends CookieThemeResolver, 并改写一个方法
    public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName),使对于改变Themename的请求,同时记录到localCokie中去。而且,如果loginUser在他自己空间修改Theme,Theme还要保存在UserAccount数据库表中。
    (如果不需要修改,1、2步可省略)
  3. Controller中,直接用request.setAttribute(CookieThemeResolver.THEME_REQUEST_ATTRIBUTE_NAME, String.valueOf(tTheme))来改变当前请求返回页面时所适用的Theme,PersonalController和其他Controller优先根据spaceOwner的设置来决定是否改变theme; 而如果没有改变,默认是通过spingMVC自动通过cokie来设。
  4. load-script.tagx中,设置从一个特殊文件中获取所需应用的css文件的名字
      <spring:theme code="styleSheet" var="roo_css" />       这时,roo_css 的值为“resources/styles/2.css”
      <spring:url value="/${roo_css}" var="roo_css_url" />     这变量值为roo_css 的值前冠予/bigbang/, 即/bigbang/resources/styles/2.css
      <link rel="stylesheet" type="text/css" media="screen" href="${roo_css_url}" />
  5. foot.jsp 中的Them 链接,负责给参数中增加一个theme=n,从而触发spingmvc的theme机制,eg.当参数中出现theme=xx字样时,springmvc的filter会调用MyCookieThemeResolver 的setThemeName方法,把新的theme存入cokie
    (可以试验一下,如果在spingmvc.xml中定义themeResolver bean为会CookieThemeResolver,此功能同样有效,只是不会在owner login状态下,把设置存入数据库表而已)
-- Sam 19:49 13/07/2014
SpingMVC Controller中的方法是顺序敏感的?
今天才知道,起因是从创建新留言JSP网页中发送请求到Controller后,发现没有没有进入预期的create方法。
因为前些天临时把Message和Remark并在一起的,当时没有发现找不到或找错方法的现象。
我 唯一的修改是在Roo 生成的create方法上加了一个参数。难道是这个原因??不可能,因为在RemarController里面时是好的。为了确信,把path参数重新改 成指向RemarkController-----仍然是好的,说明不是这个原因,同时说明问题不在网页,而在Controller方面。

MessageController死活看不错异常,就是加了参数后,create方法就进不来,去掉参数,就能进来。
最后才怀疑到在前面的那个list方法,RemarkController中也有个list方法,唯一不同是放在了后面...
于是也挪到后面去,再试-----成功!
说明:spring在Controller中匹配方法是,不是找最最匹配的,而是从上往下找第一个匹配的。
(wait!!! 这么说也不对,例子:
url:http://localhost/bigbang/tao会进入第一个方法,http://localhost/bigbang/getImage进入第一个方法,而http://localhost/bigbang/getImage/2234则会进入第二个方法。说明参数层数是个硬坎。即使第二个方法更匹配,但层数不对,就指定会跟第一个方法匹配。
Method1:
    @RequestMapping(value = "/{spaceOwner}", produces = "text/html")
    public String index(@PathVariable("spaceOwner") String p1, @RequestParam(value = "page", required = false) Integer p2) {

Method2:
    @RequestMapping(value = "/getImage/{id}")
    public void getImage(@PathVariable("id") String id,) {

另外,如果第二个方法前插一个   
   @RequestMapping(value = "/getImage")
    public void getImage()

那么,urlhttp://localhost/bigbang/getImage就会匹配到这个新插入的方法上。
而如果你输入一个没有匹配的URL,如:http://localhost/bigbang/xyz/eee
那么第一个方法将会把它截获,但是截获后你会发现p1参数的值为“resourceNotFound".
----------------------------------------------------------
 不但要保证RequestMapping 唯一性,还要保证RequestParam数目相同
that not only the @RequestMapping must be the unique one for the URLString, but also the
      @RequestParam name and numbers must match the URLstring. what we still not clear is that is it OK if the URLString contains more
      parameters than specified?
the parameter's sequence matters:
     <spring:url value="${path}" var="form_url">
        <spring:param name="twitterid" value="${twitterId}"/>
        <spring:param name="refreshTime" value="44"/>
     </spring:url>
goes to the 
    @RequestMapping(params = "twitterid", method = RequestMethod.POST, produces = "text/html")
    public String createRemark(@RequestParam(value = "refreshTime", required = false) String refreshTime, @Valid Remark remark, BindingResult bindingResult, @RequestParam(value = "twitterid", required = false) Long pTwitterId, Model uiModel, HttpServletRequest httpServletRequest) {
(which is good!)
while
     <spring:url value="${path}" var="form_url">
        <spring:param name="refreshTime" value="44"/>
        <spring:param name="twitterid" value="${twitterId}"/>
     </spring:url>
goes to:
    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel)
-- Sam 09:01 11/07/2014
如何在Spring项目中配置并使用Quartz?
Spring的配置文件如何被Tomcat找到? 
Tomcat的没有应用程序下有个WEB-INF目录,里面有个web.xml文件,里面有个注释:<!-- Context Configuration locations for Spring XML files -->这一个部分的param name value对就告诉了Tomcat到哪里去找xml文件,把里面的内容都读到hashmap里去:<param-name>contextConfigLocation</param-name>        <param-value>.......<>
Quartz如何加入到项目里?
在任意一个web.xml能找到的文件里定义
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
      <ref local="refreshSessionTrigger"/>
    </list></property> </bean>    
<bean id="refreshSessionTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="refreshSessionJobDetail" />
   <property name="startDelay">
<value>0</value>
        </property>
      <property name="repeatInterval">
<value>60000</value>
    </property></bean>
<bean id="refreshSessionJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="refreshSessionJobProcessor" />
</property>
<property name="targetMethod">
<value>refreshSession</value>
</property></bean>
<bean id="refreshSessionJobProcessor" class="com.caesarsint.box.job.RefreshSessionJobProcessor"></bean>
然后,RefreshSessionJobProcessor类中的ratgetMethod:refreshSession就会被执行了。
注意,这些bean要倒着写(因为确保每个用到的bean都是事先定义过的)
其他
quartz 2.1.6 does not work with spring 3.1 well (if we user 2.1.6, we can not user cronTrigger though we can still use SimpletriggerFactoryBean to set repeatInterval)
because the parent class of class org.springframework.scheduling.quartz.CronTriggerBean was a class in quartz.jar with the nameCronTrigger. while now, the "CronTrigger" is changed into an interface by quartz!

-- Sam 07:41 11/07/2014
 如何在spring Security中配置单点登录
  1. 在<http></http>模块中增加一个custom-filter : <custom-filter position="CAS_FILTER" ref="casFilter"/> 并定义这个casFilter:   
    <beans:bean id="casFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
           <beans:property name="authenticationManager" ref="authenticationManager"/>   
    </beans:bean>
  2. <http>模块的Entry-point-ref必须稍加修改: entry-point-ref="casEntryPoint"
        <beans:bean id="casEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
            <beans:property name="loginUrl" value="http://localhost:8089/cas/login"/>
            <beans:property name="serviceProperties" ref="serviceProperties"/>
        </beans:bean>
    意思是转发到另一个网站地址(统一地址)用serviceProperties内的信息进行登录。
  3. 定义这个serviceProperties:
        <!-- Identify our app for CAS. The service name will allow the display of a particular page on the CAS server -->
        <beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
            <beans:property name="service" value="http://localhost:8080/j_spring_cas_security_check"/>
            <beans:property name="sendRenew" value="false"/>
        </beans:bean>
-- Sam 07:40 11/07/2014
springsecurity,如何获得当前用户?
java代码中:
    @Inject
    private UserContextService userContextService;
    userContextService.getCurrentUserName()
-- Sam 07:40 11/07/2014

Please click here to login and add comments! || automatically refresh content every seconds