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

SpringMVC 注解配置

2013-07-21 20:03 447 查看
在Spring项目开发中呢,最好是搞明白原理,其次装上Spring为eclipse开发的插件,这样会大大提高开发效率,而且减少了大量信息的记忆负担。SpringIDE插件,可自行到eclipse插件库中进行下载,还有其他Spring相关的plugin可以自行研究下。当装好这个插件之后呢,可以根据向导创建一个简单的SpringMVC项目,大量的基本信息都可以自动生成,当然了是建立在明白原理的基础上,熟练了之后再去使用插件。
截个图感觉下,创建Spring项目的向导。



创建好之后的项目目录,



首先看下,web.xml。这里copy自动生成的xml讲解下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>


根据xml中的注释也能很容易各个配置的作用。





<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
项目的根容器上下文,用于Servlet和Filter的共享。
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


Context加载器。
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


SpringMVC前端控制器。相当核心的一个东西,利用了facade设计模式,有兴趣的可以研究下。

SpringMVC中request的生命周期
当每个请求到达前端控制器,前端控制开始根据相应的HandlerMapping配置,发送给Controller,Controller调用logic object或service或domain object处理,然后返回一个视图的逻辑名称和相应的model(数据),给前端控制器,然后前端控制器根据相应的视图解析(ViewResolver)配置,转给相应的视图(譬如jsp页面)。

在spring-context.xml中添加上对注解的支持。
<bean name="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean name="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


第一个主要是对类级别上的注解处理。
第二个主要是对方法级别上的注解处理。

在spring-context.xml中配置好视图解析器。
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

配置Spring的组件扫描功能。
<context:component-scan base-package="org.changsheng.springmvc" />


当基本的配置工作做好之类,下面举几个例子来说明注解的使用,当然都是很简单一目了然的例子。

一个特定的URL映射到一个控制器类
/**
* Handles requests for the application home page.
*/
@Controller
@RequestMapping("/home.jsp")
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);

Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

String formattedDate = dateFormat.format(date);

model.addAttribute("serverTime", formattedDate );

return "home";
}

}


多映射

@Controller
public class ProductController {
private ProductService productService;

@Autowired
public ProductController(ProductService productService){
this.productService = productService;
}

@RequestMapping("/product/add.jsp")
public String addProduct(Product product){
productService.addProduct(product);
return "redirect:list.htm";
}
@RequestMapping("/product/del.jsp")
public String delProduct(@RequestParam("productName") String productName){
productService.delProduct(productName);
return "redirect:list.htm";
}

@RequestMapping("/product/list.jsp")
public ModelAndView listProduct(Product product){
List<Product> products = productService.list();
return new ModelAndView("productList","products",products);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息