1. 程式人生 > >SpringMVC_第二章SpringMVC入門程式碼實現

SpringMVC_第二章SpringMVC入門程式碼實現

1:功能概述

用springmvc實現在頁面的簡單的商品儲存功能,頁面上有商品名稱和價格兩個按鈕,輸入之後提交到後臺,並且在新的頁面把資料展示出來。

點選提交之後如圖所示

2:程式碼實現如下 

新增頁面:

	<form class="form-horizontal" role="form" action="action11/addGoods" method="post">
		<input type="text" class="form-control" name="name" placeholder="請輸入商品名稱"> 
		<input type="text" class="form-control" name="price" placeholder="請輸入商品價格">
		<button type="submit" class="btn btn-default">提交</button>
	</form>

進入Controller

@Controller(value="action1")
@RequestMapping("action11")
public class action1 {
	@Autowired
	private service1 service1;
//	springMVC把前臺的資料封裝為POJO與struts2的封裝形式不同。
//	struts2需要在控制器宣告需封裝的POJO,而springMVC不需要任何準備工作,
//	只需在相應的方法的引數中加上需封裝的POJO,當用戶提交表單時,
//	springMVC會根據表單中dom元素的name屬性與請求的方法中的引數中的POJO的屬性名進行比對,
//	如果相同,
//	則將name屬性的值賦給這個屬性,進而完成封裝,下面舉例說明
	@RequestMapping("/addGoods")
	public ModelAndView addgoods(HttpServletRequest request,HttpServletResponse response,goods goods) {
		System.out.println("進入後臺方法!!!!");
		ModelAndView andView=new ModelAndView();
		System.out.println(goods.toString());
		System.out.println("name:"+goods.getName());
		andView.addObject("goods", goods);

		andView.addObject(service1.getname(), "11111111111");
		if(goods.getName()==null||("").equals(goods.getName())) {
			System.out.println("name為空!");
			andView.setViewName("add");
		}else {
			System.out.println("name不為空!");
			andView.setViewName("show");
		}
		return andView;
		
	}
	

進入Service:

@Service
public class service1 {
	@Autowired
	private dao1 dao;
	public String getname() {
		System.out.println("--service1--");
		String name=dao.getname();
		return name;
	}

}

進入Dao:

@Repository
public class dao1 {
	public String getname() {
		return "name";
	}
}

展示頁面:

<body>
	${name}
	<h1>-----------展示商品名稱---------</h1>
	${requestScope.goods.name}
	<h1>-----------展示商品價格---------</h1>
	${goods.price}
</body>

公共web.xml配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	
	<!--處理中文亂碼  -->
	<filter>
		<filter-name>encofingFilter</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>
	
	<!-- 載入js,jsp -->
	<filter-mapping>
	<filter-name>encofingFilter</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	
	
	<servlet>
		<!-- Dispatcher Servlet 排程伺服器-->
		<servlet-name>SpringMVC1</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--配置springmvc的初始化路徑 
		classpath:只會到你的class路徑中查詢找檔案; 
			classpath*:不僅包含class路徑,還包括jar檔案中(class路徑)進行查詢.
		-->
		 <init-param>
            <param-name>contextConfigLocation</param-name>
           <!--  <param-value>WEB-INF/classes/Spring/springmvc.xml</param-value>  -->
           <!--  <param-value>classpath:Spring/springmvc.xml</param-value> -->
           
            <param-value>WEB-INF/springzhueji/springmvc.xml</param-value>
        </init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>SpringMVC1</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

spring配置檔案:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!--將AnnotationHandler自動掃描到IOC容器中-->
	<context:component-scan base-package="com.thit"></context:component-scan>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
		<!--前置字尾 -->
		<property name="prefix" value="/"></property>
		<!--後置字尾  -->
		<property name="suffix" value=".jsp"></property>
	</bean>

  
</beans>

以上就是這個簡單功能的程式碼例項。

springMVC的註解配置是:

     @RequestMapping("xxxx")

    //@Repository //用於DAO實現類
    
    //@Service  //用於service
    
    //@Controller //用於action(controller)