jee、spring、spring mvc、mybatis 学习(三)

转载请申明转载出处:http://blog.csdn.net/qq5132834/article/details/52199255

在上一节中,漏掉了一个内容,那就是【主动注入】功能的实现:

1、在类文件中新建一个包:【com.zuk.services】,并在其中新建一个类【PersonService.java】,其中要特别注意【@Componet】,必须添加上。内容如下:

package com.zuk.services;

import org.springframework.stereotype.Component;

@Component
public class PersonService {

	public final static String TAG = "com.zuk.services.PersonService";
	
	public PersonService(){
		
	}
	
	public void showTag(){
		System.out.println(TAG);
	}
	
}


2、在【mvc-servlet.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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  
    http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 
     default-autowire="byName" >
     
     
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.zuk.services" />
<!-- 扫描com.zuk.services这个包里所以的service类,【务必在类上标注:@Component】,此时可以采用@Autowired实现自动注入这个包里面全部的对象     -->
      
<context:component-scan base-package="com.zuk.controllers" />
<!-- 扫描com.zuk.controllers这个包里的所以controller类,【务必在类上标注:@Controller】 -->

   
<!-- ViewResolver -->  
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
    <property name="prefix" value="/WEB-INF"/><!-- 也可以放空 -->
    <!-- <property name="prefix" value="/WEB-INF/jsp/"/> --> <!-- 制定前缀字符串 -->
    <property name="suffix" value=".jsp"/>  <!-- 跳转后添加后缀 -->
</bean> 


<!-- ajax请求中文乱码解决 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	<property name="messageConverters">
		<list>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/plain;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</list>
	</property>
</bean>



</beans>

其中数据是:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.zuk.services" />
<!-- 扫描com.zuk.services这个包里所以的service类,【务必在类上标注:@Component】,此时可以采用@Autowired实现自动注入这个包里面全部的对象     -->

3、在【HelloController.java】代码中实现自动注入:

@Autowired
public PersonService personService;

通过加上【@Atuowired】方式实现自动注入,

不需要用【public PersonService personService = new PersonService();】,

调用PersonService类中的方法【personService.showTag(); 】

全部源代码如下:

package com.zuk.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuk.model.Person;
import com.zuk.services.PersonService;

/**
 * @author 513283439@qq.com
 * */
@Controller
@RequestMapping(value="HelloController")
public class HelloController {
	 
	@Autowired
	public PersonService personService;

	@RequestMapping(value = "/hello.xhtml")
	public String hello(HttpSession ession,
						HttpServletRequest request,
						HttpServletResponse response,
						Model model,
						@RequestParam(value="message", required=false) String message){
		
		personService.showTag();
		System.out.println(message);
		
		model.addAttribute("message", message);
		return "/jsp/hello";  
		/**
		 * 跳转的实际路径要算上mvc-servlet.xml中配置的
		 * 前缀:/WEB-INF;
		 * 后缀:.jsp
		 * 实际路径:/WEB-INF/jsp/hello.jsp
		 * */
	}
	
	/**
	 * <br>主要解决两个问题:
	 * <br>1、POST方法中文乱码问题;
	 * <br>2、将前端表单里面的name属性的值直接映射到java对象中。
	 * */
	@RequestMapping(value = "/actionForm.xhtml")
	public String actionForm(HttpSession ession,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model,
			@ModelAttribute Person person
			){
		
		System.out.println(person.getUsid());
		System.out.println(person.getPawd());
		System.out.println(person.getComp());
		
		model.addAttribute("usid", "POST中文乱码-->>"+person.getUsid());
		model.addAttribute("pawd", "POST中文乱码-->>"+person.getPawd());
		model.addAttribute("comp", "POST中文乱码-->>"+person.getComp());
		
		return "/jsp/hello";
	}

	/**
	 *<br>主要解决ajax请求返回json数据烈性 
	 * */
	@ResponseBody 
	@RequestMapping(value = "/ajaxForm.json", produces = "text/html;charset=UTF-8")
	public String ajaxForm(HttpSession ession,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model,
			@ModelAttribute Person person
			){
		
		System.out.println(person.getUsid());
		System.out.println(person.getPawd());
		System.out.println(person.getComp());
		
		model.addAttribute("usid", "ajaxForm-->>"+person.getUsid());
		model.addAttribute("pawd", "ajaxForm-->>"+person.getPawd());
		model.addAttribute("comp", "ajaxForm-->>"+person.getComp());
		
		NutMap result = NutMap.NEW();
		result.put("data", person);
		
		String json = Json.toJson(result);
		
		System.out.println(json);
		
		return json;
	}
	
	
}


4、源代码地址:http://download.csdn.net/detail/qq5132834/9602471








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值