1.web.xml
<!--Spring MVC 相关配置--> <servlet> <servlet-name>controller</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/spring-mvc-controller.xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>controller</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
在源文件夹下新建spring文件键创建spring-mvc-controller.xml
2.spring-mvc-controller.xml
<?xml version="1.0" encoding="GBK"?> <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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 对项目中的所有类进行扫描以完成Bean创建并进行自动注入。 支持 @Controller、@Service 以及 @Repository 和 @Component --> <context:component-scan base-package="org.leopard.controller" /> <!-- jsp视图解析器,对模型视图名称的解析,即在模型视图名称添加前后缀 如/view/welcome.jsp对应的视图名就是:welcome 这个类在视图解析器链中一定要定义在其他视图的后面,因为它始终会返回一个视图 --> <!-- 对service层下的所有类进行检测,如果发现有相关的注解,使注解生效 --> <context:component-scan base-package="org.leopard.service" /> <context:component-scan base-package="org.leopard.dao" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.控制层的配置
package org.leopard.controller.product;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.leopard.model.product.Product;
import org.leopard.service.product.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller //表示控制层
public class ProductController {
@Autowired //自动注入
private ProductService productService;
@RequestMapping("/product/toAddProduct.do")
public ModelAndView toAddProduct() {
ModelAndView view = new ModelAndView();
view.setViewName("addProduct");
return view;
}
@RequestMapping("/product/addProduct.do")
public String addProduct(Product product, String productName, float productPrice, HttpServletRequest request, HttpServletResponse rsponse, ModelMap modelMap) {
System.out.println(product.getProductName()+":"+product.getProductPrice()+":"+productName+":"+productPrice+":"+request.getParameter("productName"));
productService.addProduct(product);
return "redirect:/product/productList.do";
}
@RequestMapping("/product/productList.do")
public ModelAndView productList(ModelMap modelMap) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userName", "yuxy");
modelMap.put("userAge", 10);
modelAndView.setViewName("productList");
modelAndView.addObject(modelMap);
return modelAndView;
}
}