学习springmvc的注释搭建框架
2017-01-10 10:38
344 查看
今天来整合一下springMVC注释搭建,在使用了注释之后,开发效率明显提高了,与strut2相差无几,甚至在运行效率上还要比strut快一点。
第一步。导入相关的包至lib文件夹。
![](https://img-blog.csdn.net/20170110105005401?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3VxaWFua3VuMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
第二步。在web.xml中配置dispatcherServlet。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
第三步。添加xxx-servlet.xml文件
首先添加此bean。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
启动注解驱动的Spring MVC功能。
<mvc:annotation-driven
/>
启动包扫描功能
<context:component-scan base-package="com.springmvc.controller"
/>
配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp" />
第四步。创建controller。
![](https://img-blog.csdn.net/20170110111018524?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3VxaWFua3VuMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
@Controller
public class HelloWorld {
@RequestMapping(value="/welcome")
public String Welcome(){
System.out.println("welcome bro!!!");
return "welcome";
}
}
其中@controller标志这个类可以作为请求处理类 ,也就是 控制类。@RequestMapping用来处理对应的请
求,其中的value为请求的名字,header可以设置请求头,method可以设置提交的方式,post或者get。
jsp页面就省略了,因为此不为重点
第一步。导入相关的包至lib文件夹。
第二步。在web.xml中配置dispatcherServlet。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
第三步。添加xxx-servlet.xml文件
首先添加此bean。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
启动注解驱动的Spring MVC功能。
<mvc:annotation-driven
/>
启动包扫描功能
<context:component-scan base-package="com.springmvc.controller"
/>
配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp" />
第四步。创建controller。
@Controller
public class HelloWorld {
@RequestMapping(value="/welcome")
public String Welcome(){
System.out.println("welcome bro!!!");
return "welcome";
}
}
其中@controller标志这个类可以作为请求处理类 ,也就是 控制类。@RequestMapping用来处理对应的请
求,其中的value为请求的名字,header可以设置请求头,method可以设置提交的方式,post或者get。
jsp页面就省略了,因为此不为重点
相关文章推荐
- 学习springmvc的注释搭建框架
- SpringMvc[学习手记]-框架搭建
- Spring学习之SpringMVC框架快速搭建实现用户登录功能
- Spring学习之SpringMVC框架快速搭建实现用户登录功能
- springMVC 框架搭建学习
- Springmvc框架搭建与学习
- Spring学习之SpringMVC框架快速搭建实现用户登录功能
- springMVC学习笔记(一) ---- 搭建框架
- 【框架学习】SpringMVC原理及搭建
- Spring学习之SpringMVC框架快速搭建实现用户登录功能
- Spring学习之SpringMVC框架快速搭建实现用户登录功能
- dubbo学习笔记3 SpringMvc框架web形式 dubbo服务搭建 消费者
- springMVC学习笔记---day02 springMVC+spring+mybatis整合开发框架搭建
- SpringMVC学习(一)--框架的搭建
- SpringMVC学习(一)--框架搭建
- springMVC学习-SpringMVC框架在项目中的搭建
- spring学习(1)Spring MVC框架搭建
- Lua学习2 Lua小框架的搭建
- Lua学习2 Lua小框架的搭建
- SpringMvc项目框架的搭建