Spring 1일차 간단한 계산기

DataSend.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Start</title>
</head>
<body>
<form action="Cal.do" method="post">
<input type="text" name="num1">
<select name="oper">
<option value="add">+</option>
<option value="sub">-</option>
<option value="mul">*</option>
<option value="div">/</option>
</select>
<input type="text" name="num2">
<input type="submit" value="send">
</form>
</body>
</html>

Cal.do

Cal.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Cal
{
@RequestMapping("/Cal")
public ModelAndView printHello(int num1, int num2, String oper)
{
//model과 view를 동시에 지정 가능한 객체 선언
ModelAndView mav = new ModelAndView();

int result = 0;
if(oper.equals("add"))
result = num1 + num2;
else if(oper.equals("sub"))
result = num1 - num2;
else if(oper.equals("mul"))
result = num1 * num2;
else if(oper.equals("div"))
result = num1 / num2;

//결과를 보여줄 jsp파일명을 설정
mav.setViewName("ResultCal");
//jsp로 떠널길 데이터를 부착
mav.addObject("result", result);

return mav;
}
}

resultCal.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>result</title>
</head>
<body>
<center>
<h2>result</h2>
결과는  ${result}
</center>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <servlet>
    <servlet-name>calex</servlet-name>
    <servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>calex</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

calex-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--Controller 작성 -->
<bean id="calex" name="Cal.do" class="controller.Cal">

</bean>
<!--Controller실행후 결과를 리절트할 jsp페이지를 설정 -->
<bean id="internalResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>


result) 


댓글 없음: