spring mvc annotation 的例子

4 篇文章 0 订阅
1 篇文章 0 订阅

从表单到Controller::

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-store, must-revalidate" />
<meta http-equiv="expires" content="wed, 26 feb 1997 08:21:57 gmt" />
<meta http-equiv="expires" content="0" />
<title></title>
<script type="text/javascript" src="editor/kindeditor.js"></script>
<link rel="stylesheet" href="editor/themes/default/default.css"
	type="text/css"></link>
<script type="text/javascript" src="editor/lang/zh_CN.js"></script>
<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>

<script type="text/javascript">
	$(function() {

		KindEditor.create("textarea[name='content']", {
			resizeType : 1,
			allowPreviewEmoticons : false,
			cssPath : "editor/plugins/code/prettify.css",
			uploadJson : 'editor/jsp/upload_json.jsp',
			fileManagerJson : 'editor/jsp/file_manager_json.jsp',
			allowPreviewEmoticons : false,
			items : [

			'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
					'italic', 'underline', 'copy', 'cut', 'paste', 'selectall',
					'wordpaste', 'removeformat', '|', 'justifyleft',
					'justifycenter', 'justifyright', 'insertorderedlist',
					'formatblock', 'table', 'fullscreen', 'code', 'insertfile',

					'insertunorderedlist', '|', 'emoticons', 'image', 'media',
					'link', 'preview' ]
		});

	});
</script>
<style>
body {
	background-image: url(css/res/image/8F1F1.png);
}

#wraper {
	position: relative;
	width: 69%;
	margin-left: 13%;
	line-height: 45px;
	font-family: "Microsoft Yahei", "微软雅黑", Tahoma, Arial, Helvetica,
		STHeiti;
	_font-family: Tahoma, Arial, Helvetica, STHeiti;
	cursor: default;
	width: 69%;
}
</style>

</head>
<body>
	<div id='wraper'>

		<form action="publishNews.do" method="post" name="publishForm" id="publishForm">
			<div>
				<label for="topicName"> 新闻标题: </label> <input name="title"
					id="topicName" type="text" /> <span
					style="display: inline-block; padding: 4px 0 0 10px;">*************************</span>
				<br /> <label for="headline" style="color: gray; font-size: 16px;">
					头条新闻 </label> <input type="checkbox" name="headline" id="headline" />
			</div>
			<br />
			<textarea class='alex-input' name="content"
				style="width: 99%; height: 200px; visibility: hidden; margin-top: 20px;"> </textarea>

			<input id="btn_submit" name="btn_submit" type="submit"
				style="float: right;" />
		</form>

	</div>

</body>
</html>

Controller:

package com.spring.controller;

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.RequestMethod;

import com.spring.model.News;
import com.spring.service.NewsManagerService;

@Controller
@RequestMapping("/publishNews")
public class publishNewsController {

	@Autowired
	private NewsManagerService newsManagerService;

	@RequestMapping(method = RequestMethod.POST)
	public String publish(@ModelAttribute("publishForm") News news, Model model) {

		this.newsManagerService.save(news);

		model.addAttribute("content", news.getContent());

		return "success";
	}

}

 

Spring MVC 有用于表单绑定的标签。但这些标签最终也会生成标准的 HTML 页面。所以,理论上这些标签是不需要的,只要模拟它们生成的 HTML 就可以了。


 Spring MVC 的 Form 标签生成的 HTML 很有规律:<form> 元素的 id 属性对应 modelAttribute,input 元素的 name 属性对应 model 中的属性。

 

Model:

package com.spring.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name = "news")
public class News {

	@Id
	@GeneratedValue
	private int id;

	@Column(name = "title", length = 100)
	private String title;

	@Lob
	@Column(name = "content")
	private String content;

	public String getContent() {
		return content;
	}

	public int getId() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}


 

Service:

package com.spring.service;

import com.spring.model.News;

public interface NewsManagerService {

	public abstract void save(News news);
}

package com.spring.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.spring.dao.NewsDao;
import com.spring.model.News;
import com.spring.service.NewsManagerService;

@Service("newsManagerServiceImpl")
@Scope("prototype")
public class NewsManagerServiceImpl implements NewsManagerService {

	@Autowired
	private NewsDao newsDao;

	@Override
	public void save(News news) {
		this.newsDao.save(news);
	}

}


 

 

package com.spring.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.spring.dao.NewsDao;
import com.spring.model.News;
import com.spring.service.NewsManagerService;

@Service("newsManagerServiceImpl")
@Scope("prototype")
public class NewsManagerServiceImpl implements NewsManagerService {

	@Autowired
	private NewsDao newsDao;

	@Override
	public void save(News news) {
		this.newsDao.save(news);
	}

}


 

DAO:

 


 

package com.spring.dao;

import com.spring.model.News;

public interface NewsDao {

	public abstract void save(News news);
}


 

package com.spring.dao.impl;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.spring.model.News;

@Repository("newsDaoImpl")
public class NewsDaoImpl implements com.spring.dao.NewsDao {

	@Autowired
	private HibernateTemplate hibernateTemplate;

	private Logger logger;

	@Override
	public void save(News news) {
		if (news != null) {
			this.hibernateTemplate.save(news);

		} else {
		}
	}
}



嗨嗨,熟悉不过的这些MVC模式吧,

这个@Autowired第一次用就很成功,很方便,和@Resouce的区别就是不用setter.


这个测试很成功,呵呵,也证明我的学习能力还不错哦。

再贴spring-beans.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:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/jee
						http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
						http://www.springframework.org/schema/context
           				http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />

	<!-- 把标记了@Controller注解的类转换为bean -->
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

	<context:component-scan base-package="com.spring.controller" />

	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/view/" p:suffix=".jsp">
	</bean>


	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="defaultErrorView" value="failure" />
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">/exception/exception</prop>
			</props>
		</property>
	</bean>

	<!-- lets use the Commons-based implementation of the MultipartResolver 
		interface -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		p:defaultEncoding="utf-8">
	</bean>

	<!-- RMI -->
	<bean id="accountServiceImpl" class="com.spring.service.impl.AccountServiceImpl" />

	<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="serviceName" value="AccountService" />
		<property name="service" ref="accountServiceImpl" />
		<property name="serviceInterface" value="com.spring.service.AccountService" />
		<property name="registryPort" value="1199" />
	</bean>


</beans>




 


起到annotaion作用的几句话:起到annotaion作用的几句话:起到annotaion作用的几句话:起到annotaion作用的几句话:起到annotaion作用的几句话:

	<context:annotation-config />

	<!-- 把标记了@Controller注解的类转换为bean -->
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

	<context:component-scan base-package="com.spring.controller" />

	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

看起来真不错这个纯spring mvc,感觉很轻松。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值