SpringMvc 프레임 워 크 (컨트롤 러)

8507 단어 자바
실체 클래스: User. java
package test1;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private double sal;
	private Date hiredate;
	......
}


프로필 웹. xml


  demo1
  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
  
  
    DispatcherServlet
    *.action
  
  
  
    CharactorFilter
    test.CharactorFilter
    
      encoding
      UTF-8
    


  	CharactorFilter
    /*

  


프로필 springmvc. xml


	
	
	
	
	
	
	
		
		
	

      

페이지





Insert title here







정상 컨트롤 러 파일
package test1;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloAction {

	/*
	 *     
	 *    /hello.action,a.action,b.action   ,   HelloAction    hello()     
	 * value=    
	 * */
	@RequestMapping(value= {"/hello.action","/a.action","/b.action"})
	public String hello(Model model) throws Exception{
		
		model.addAttribute("message","hello,helli,hello");
		
		//  
		return "success";
	}
	
	@RequestMapping(value= {"/bye.action","/c.action","/d.action"})
	public String bye(Model model) throws Exception{
		System.out.println("   /hello.action   ,   HelloAction    hello()     ");
		
		model.addAttribute("message","bye,bye,bye");
		
		//  
		return "success";
	}
	
}


매개 변수 수집
package test1;

import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/user")
public class UserAction {

	/*
	 *     
	 * 
	 *                   
		1)          ,            ,springmvc         
	 * 
	 *           ,   GET POST      
		1)          ,             GET POST   
	 * */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(Model model,String username,Double salary) throws Exception{
		System.out.println(username+":"+salary);
		model.addAttribute("message", "      ");
		return "success";
	}
	
	/*
	 *     
	 * */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(Model model,String username,Double salary) throws Exception{
		System.out.println(username+":"+salary);
		model.addAttribute("message", "      ");
		return "success";
	}
	
	/*
	 *           Request,Response   web  
		1)              web  ,         ,   
	 * */
	@RequestMapping(value="/registerANDlogin")
	public void registerANDlogin(HttpServletRequest request,HttpServletResponse response) throws Exception{
		request.getParameter("username");
		request.getParameter("salary");
		response.sendRedirect(request.getContextPath()+"/register.jsp");
	}
	
	
	/*
	 *           User,Admin        
		1)            1            
		2)                    
		3)        Model  ,Model          
	 * */
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(
				Date.class, 
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(User user,Model model) throws Exception {
		System.out.println(user.getId()+":"+user.getName()+":"+user.getSal()+":"+user.getHiredate());
		model.addAttribute("user",user);
		return "login";
	}
	
	/*
	 *             User        
		            0               
		1)              ,   user.name admin.name        
		2)        User Admin     
	 * */
	@RequestMapping(value = "/User", method = RequestMethod.POST)
	public String add(Bean bean,Model model) throws Exception {
		System.out.println(bean.getUser());
		//System.out.println(bean.getAdmin());
		System.out.println("PersonAction::add()::POST");
		model.addAttribute("bean",bean);
		return "/success.jsp";
		
		/*
		 *     :
		 * 	${bean.user.id }
		 * */
	}
	
	/*
	 *               
		      
	 * */
	@RequestMapping(value="/delete")
	public String deleteMethod(int[] ids,Model model) throws Exception{
		System.out.println("UserAction::deleteMethod()");
		System.out.println("     id :");
		for(int id : ids){
			System.out.print(id+" ");
		}
		model.addAttribute("message","      ");
		return "/success.jsp";
}

package test1;

public class Bean {

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}

좋은 웹페이지 즐겨찾기