본문 바로가기
Back-end/Spring Framework

java spring , Controller 설정팁

by javapp 자바앱 2020. 8. 9.
728x90

 

 

<.html>

 

 

       <h1>Member Join</h1>

       <form action="/lec17/memJoin" method="post">

             ID : <input type="text" name="memId" ><br />

             PW : <input type="password" name="memPw" ><br />

             MAIL : <input type="text" name="memMail" ><br />

             PHONE : <input type="text" name="memPhone1" size="5"> -

                           <input type="text" name="memPhone2" size="5"> -

                           <input type="text" name="memPhone3" size="5"><br />

             <input type="submit" value="Join" >

             <input type="reset" value="Cancel" >

       </form>

       <a href="/lec17/resources/html/login.html">LOGIN</a> &nbsp;&nbsp; <a href="/lec17/resources/html/index.html">MAIN</a>

 

 

 

<MemberController.java>

 

@Controller

//@RequestMapping("/member")  // /lec17/member/memJoin   : member 겹쳐서 있을

public class MemberController {

//     MemberService service = new MemberService();

//     @Autowired

       @Resource(name="memService")

       MemberService service;

      

//  @RequestMapping("/memJoin") //GET 방식일 경우

       @RequestMapping(value="/memJoin", method=RequestMethod.POST)

//     public String memJoin(Model model, HttpServletRequest request)

       public String memJoin(Member member)

       {

/*           String memId = request.getParameter("memId");

             String memPw = request.getParameter("memPw");

             String memMail = request.getParameter("memMail");

             String memPhone1 = request.getParameter("memPhone1");

             String memPhone2 = request.getParameter("memPhone2");

             String memPhone3 = request.getParameter("memPhone3");*/

            

             service.memberRegister(member.getMemId(), member.getMemPw(), member.getMemMail(), member.getMemPhone1(),

                           member.getMemPhone2(), member.getMemPhone3());

      

             /*

              * model.addAttribute("memId", memId); model.addAttribute("memPw", memPw);

              * model.addAttribute("memMail", memMail); model.addAttribute("memPhone",

              * memPhone1 + " - " + memPhone2 + " - " + memPhone3);

              */

            

             return "memJoinOk"; //.jsp

       }

}

 

 

<memJoinOk.jsp>

<%--   ID : ${memId}<br />

       PW : ${memPw}<br />

       Mail : ${memMail} <br />

       Phone : ${memPhone} <br /> --%>

      

       <!-- 커멘드 객체를 이용한 방법 -->

       <p>ID : ${member.memId}</p>

       <p>PW : ${member.memPw}</p>

       <p>Mail : ${member.memMail}</p>

       <p>phone : ${member.memPhone1}-${member.memPhone2}-${member.memPhone3}</p>

      

 

 


<controller> 

      public String memJoin(@ModelAttribute("mem") Member member)   

 

이렇게 설정하면 jsp에서 설정한 이름으로 불러올 수 있다.

 

       <p>ID : ${mem.memId}</p>

 


<controller>

       @ModelAttribute("serverTimef")

       public String getServerTime(Locale locale)

       {

             Date date = new Date();

             DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

             return dateFormat.format(date);

       }

 

함수위에 ModelAttribute 를 설정하면 

언제든 사용가능 

 

 

<p> ${serverTimef} </p>

 

 


 

 

PHONE : <input type="text" name="memPhone[0].memPhone1" size="5"> -
<input type="text" name="memPhone[0].memPhone2" size="5"> -
<input type="text" name="memPhone[0].memPhone3" size="5"><br />

.

.

.

 

 

 

<Member>

 

private List<MemPhone> memPhone;

 

 

<MemPhone>

private String memPhone1;

private String memPhone2;

private String memPhone3;

 

 


model 대신 ModelAndView 쓸 경우

 

 

       @RequestMapping(value = "/memModify", method = RequestMethod.POST)

       public ModelAndView memModify(Member member) {

             Member[] members = service.memberModify(member);

            

             ModelAndView mav = new ModelAndView();

             mav.addObject("memBef", members[0]);

             mav.addObject("memAft", members[1]);

             mav.setViewName("memModifyOk");

             return mav;

       }

 

댓글