javabean。xml。springmvc。

         以前jsp配合flex,选用的是springmvc框架,将view层用xml表示。现在直接采用castor,并在response的输出流直接输出xml

         Controller和页面效果如下:

 

 

package mvc.login;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

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

import mvc.controllerimp.MControllerImp;

import org.exolab.castor.xml.Marshaller;
import org.springframework.web.servlet.ModelAndView;

import basic.Dealer;
import basic.JspVAR;
import basic.User;

public class Log_inController extends MControllerImp {
	private LoginUtil loginUtil;

	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		/**
		 * */
		String[][] members = { { "username", "String" },
				{ "password", "String" } };
		Object[] elements = Dealer.getElemByName(request, members);
		LoginForm loginfrm = new LoginForm(elements);
		//
		logger.info("username is: " + loginfrm.getUsername());
		logger.info("password is: " + loginfrm.getUsername());
		logger.info(request.getContextPath());
		// return new ModelAndView("LoginXML");
		/**
		 * 
		 */
		User[] users = loginUtil.judgeUserName_Password(loginfrm);
		if (users.length == 1) {
			request.getSession().setAttribute(JspVAR.session_user,
					new User(loginfrm.getUsername()));

		}
		return new ModelAndView("LoginXML", "result", users.length);
	}

	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		request.getSession().setAttribute(JspVAR.session_user, null);
		request.getSession().setAttribute(JspVAR.session_date, null);

		request.getSession().invalidate();

		logger.info(JspVAR.redirect + JspVAR.loginjsp);
		return new ModelAndView(JspVAR.redirect + JspVAR.loginjsp);
	}

	public void castorGenerate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		FlightBean bean1 = new FlightBean();
		bean1.setCarrier("AR1");
		bean1.setNumber(426);
		bean1.setDepartureTime("6:23a1");
		bean1.setArrivalTime("8:42a1");
		//
		FlightBean bean2 = new FlightBean();
		bean2.setCarrier("AR2");
		bean2.setNumber(426);
		bean2.setDepartureTime("6:23a2");
		bean2.setArrivalTime("8:42a2");
		//
		Routebean bean = new Routebean();
		bean.setFrom("from");
		bean.setTo("to");
		bean.addFlight(bean1);
		bean.addFlight(bean2);

		try {
			response.setContentType("text/xml; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			PrintWriter out = response.getWriter();
			// JSONObject jobject = new JSONObject();
			// JSONArray array = new JSONArray();
			// for (int i = 0; i < array1.length; i++) {
			// array.add(array1[i]);
			// }
			// jobject.put("list", array.toString());
			// System.out.println(jobject.toString());
			// out.print(jobject.toString());
			Writer writer = new StringWriter();
			Marshaller.marshal(bean, writer);
			out.print(writer.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public LoginUtil getLoginUtil() {
		return loginUtil;
	}

	public void setLoginUtil(LoginUtil loginUtil) {
		this.loginUtil = loginUtil;
	}

}

 

 



 

 

 

你可能感兴趣的:(xml,bean,mvc,servlet,Flex)