Spring3 MVC 学习笔记(一) 入门

spring mvc原理图:


*根据HTTP请求的URL,调用相应的DispatcherServlet控制器。
*提供一个视图是作为HTTP响应发送。

 

【springmvc 入门例子】

 

导入需要的包

 

文件目录:


 1.创建HelloWorldController java文件

 

package org.spring.mvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {
	private Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
	@RequestMapping("/helloWorld")
	public ModelAndView helloWorld() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("helloWorld");
		mav.addObject("message", "Hello World!");
		logger.info("this is helloWorld");
		return mav;
	}
}

 

2.spring_mvc.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:p="http://www.springframework.org/schema/p"
	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.spring.mvc" />
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

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

	<!--  对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析-->	
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/" /><!-- 跳转页面的前缀路径 如 /web-inf/user/ -->
		<property name="suffix" value=".jsp"></property><!-- 跳转页面后缀 如 helloWorld.jsp-->
	</bean>

	<!-- 中文编码处理 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource"
		p:basename="i18n/messages" />

</beans>

   

3.log4j.properties文件

 

#log4j.rootLogger = [ level ] , appenderName, appenderName, ...
log4j.rootLogger = INFO, console, R
#level=INFO,all can be output
#console is set to be a ConsoleAppender
log4j.appender.console = org.apache.log4j.ConsoleAppender
#console have four patterns
#org.apache.log4j.HTMLLayout
#org.apache.log4j.PatternLayout
#org.apache.log4j.SimpleLayout
#org.apache.log4j.TTCCLayout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
#define the output type
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
#file is set to output to a extra file
log4j.appender.R = org.apache.log4j.RollingFileAppender
#the absolute route of the log4j file
log4j.appender.R.File=${webName.root}/WEB-INF/logs/log.log  
#the size
log4j.appender.R.MaxFileSize = 500KB
#back up a file
log4j.appender.R.MaxBackupIndex = 1
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] - %m%n

 

4.helloWorld.jsp文件

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%
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 'helloWorld.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>
    This is my helloWorld page. <br>
  </body>
</html>

 

5.浏览器访问如图所示,根据url跳转到所在action并转向到目标页面


HelloWorldController 跳转说明:

 

(1)使用无返回方法跳转,如果使用返回方法进行跳转的话,则会通过视图解析器进行以prefix(前缀)+方法名+suffix(后缀)组成的页面文件名称.这样的话可能需要用url writer框架进行重向地址更改,保证安全性。

 

(2)使用一个返回的字符串方法作为跳转,使用字符串跳转的话好处就是在return的时候可以自己指定返回的名字,JSP组成是prefix(前缀)+返回的字符串+suffix(后缀),这样的话跳转的时候外围广很多而且,安全性相对高点。

 

(3)返回一个ModelAndView类型,ModelAndView是spring2.5经常使用的方式,使用setViewName方法则可以跳转到指定的页面。

 

在类中还还发现一个Contorller的注解,该注解是用于把当前Java类变成一个Spring里面的一个beans,如果在Class 上面加入requestMapping的话,而在方法上还拥有requestMapping,则在浏览器输入的地址为http://访问地址:端口/contextroot/要调用类的requestMapping跳转值/该类中存在的requestMapping/,而在Class上标记了requestMapping的话,则需要在指定的位置创建一个文件夹作为存放.

 

 

 

本人开了个充值淘宝网店。有需要的朋友请访问的店铺并拍下所充值的话费,

本店已加入消费保障服务计划,货源来源于淘宝充值平台,安全可靠便捷,

支付过后立即到账

http://xiaowen168.taobao.com

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值