Spring MVC系列:(3)控制器Controller


一、ParameterizableViewControllerhtml

org.springframework.web.servlet.mvc.ParameterizableViewControllerjava

若是請求是/index.action的請求路徑,則直接跳轉到/index.jsp頁面,不通過程序員定義的控制器Action。從另外一角度來理解,就是:能夠隱藏真正訪問.jsp文件,而使用.action進行訪問。程序員

    <!-- /index.action請求,直接轉發到/index.jsp頁面 -->
    <bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
    	<property name="viewName" value="/index.jsp"></property>
    </bean>



ParameterizableViewControllerweb

org.springframework.web.servlet.mvc.ParameterizableViewControllerspring

This controller offers an alternative to sending a request straight to a view such as a JSP. The advantage here is that the client is not exposed to the concrete view technology but rather just to the controller URL; the concrete view will be determined by the ViewResolver. mvc




二、AbstractCommandControllerjsp

org.springframework.web.servlet.mvc.AbstractCommandControlleride

可以以實體的形式,收集客戶端參數post

wKioL1ffCHPwl4f-AABmD0QpHGQ488.png


(1)Action類繼承AbstractCommandController測試

EmpAction.java

package com.rk.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.rk.web.entity.Employee;

public class EmpAction  extends AbstractCommandController{
	public EmpAction(){
		this.setCommandClass(Employee.class);
	}
	
	@Override
	protected ModelAndView handle(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		System.out.println("EmpAction.handle()");
		ModelAndView modelAndView = new ModelAndView();
		Employee emp = null;
		if(command instanceof Employee){
			emp = (Employee) command;
		}
		modelAndView.addObject("id", emp.getId());
		modelAndView.addObject("name", emp.getName());
		modelAndView.addObject("gender", emp.getGender());
		modelAndView.addObject("hiredate", emp.getHiredate());
		
		modelAndView.setViewName("/jsp/success.jsp");
		return modelAndView;
	}

}


(2)配置 

<bean name="/emp.action" class="com.rk.web.action.EmpAction"></bean>


(3)JSP頁面

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="content-type" content="text/html; charset=UTF-8">
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    <form action="${pageContext.request.contextPath}/emp.action" method="post">
    	<table>
    		<tr>
    			<td>員工ID:</td>
    			<td><input name="id" type="text"/></td>
    		</tr>
    		<tr>
    			<td>員工姓名:</td>
    			<td><input name="name" type="text"/></td>
    		</tr>
    		<tr>
    			<td>員工性別:</td>
    			<td><input name="gender" type="text"/></td>
    		</tr>
    		<tr>
    			<td>入職時間:</td>
    			<td><input name="hiredate" type="text"/></td>
    		</tr>
    		<tr>
    			<td colspan="2" align="center">
    				<input type="submit" value="添加"/>
    			</td>
    		</tr>
    	</table>
    </form>
  </body>
</html>


/jsp/success.jsp

<%@ page language="java"  pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="content-type" content="text/html; charset=UTF-8">
    <title>My SpringMVC</title>
  </head>
  
  <body>
    當前頁面:success.jsp<br>
   <table border="1" style="border-collapse: collapse;" align="center">
   	<tr>
   		<th width="200">屬性</th>
   		<th width="200">值</th>
   	</tr>
   	<tr>
   		<td>ID:</td>
   		<td>${id}</td>
   	</tr>
   	<tr>
   		<td>姓名:</td>
   		<td> ${name}</td>
   	</tr>
   	<tr>
   		<td>性別:</td>
   		<td>${ gender}</td>
   	</tr>
   	<tr>
   		<td>入職:</td>
   		<td>${ hiredate}</td>
   	</tr>
   </table> 
  </body>
</html>


(4)entity類

Emloyee.java 其中4個Field,包括三種類型:Integer、String、Date

package com.rk.web.entity;

import java.util.Date;

public class Employee {
	private Integer id;
	private String name;
	private String gender;
	private Date hiredate;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", gender=" + gender
				+ ", hiredate=" + hiredate + "]";
	}
	
}


(5)測試結果:


→對於中文不支持

→對於日期不支持

wKioL1ffC1GzEcniAAU1q3bixfg736.gif

相關文章
相關標籤/搜索