您的位置:首页 > 编程语言 > Java开发

Spring MVC入门实例

2015-08-10 21:17 519 查看


1.web.xml配置

[code]<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
'target='_blank'>http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"version="3.0">
<display-name>myweb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
	<!--加载所有的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-*.xml</param-value>
</context-param>
	<!--配置Spring监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
	<!--配置字符集-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
	<!--配置SpringMVC-->
<servlet>
<description>myweb</description>
	<display-name>myweb</display-name>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
	<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
[/code]


2.spring-mvc配置

[code]<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
'target='_blank'>http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
'target='_blank'>http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
'target='_blank'>http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
'target='_blank'>http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!--开启SpringMVC注解-->
	<mvc:annotation-driven/>
	<!--处理静态资源-->
	<mvc:resourceslocation="/resources"mapping="/resources/**"/>
	<!--打开Spring的Annotation支持-->
	<context:annotation-config/>
	<!--注解扫描包-->
	<context:component-scanbase-package="com.myweb.controller"/>
	<!--ViewResolver-->
	<!--默认的视图解析器在上边的解析错误时使用(默认使用html)--->
	<beanid="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<propertyname="prefix"value="/WEB-INF/jsp/"/>
		<propertyname="suffix"value=".jsp"/>
</bean>
</beans>
[/code]


3.HelloController类

[code]packagecom.myweb.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.servlet.ModelAndView;
@Controller
publicclassHelloController{
@RequestMapping(value="/hello",method=RequestMethod.GET)
publicModelAndViewprintWelcome(){
ModelAndViewmv=newModelAndView();
mv.setViewName("welcome");
mv.addObject("message","youarewelcome");
returnmv;
}
}
[/code]


4.welcome.jsp

[code]<%@pagelanguage="java"contentType="text/html;UTF-8"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;UTF-8">
<title>Inserttitlehere</title>
</head>
<body>
${message}
</body>
</html>
[/code]


5.eclipse的目录结构图






6.访问http://localhost:8080/myweb/hello。输出:

[code]youarewelcome
[/code]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: