- package com.benx;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- @Controller
- @RequestMapping ( "/person" )
- public class PersonController {
- PersonService service = new PersonService();
- /**
- * 进入登陆页面 http://localhost:8080/person/login 模拟
- *
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/login" , method = RequestMethod.GET)
- public String initLogin(HttpServletResponse response) throws IOException {
- response.setContentType("text/html;charset=utf-8" );
- response.getWriter().println("进入登陆页面" );
- response.flushBuffer();
- return null ;
- }
- /**
- * 登陆 模拟http://localhost:8080/Web/person/login?method=login&name=person1&pass=1
- * 使用@ModelAttribute 把paramter转换为pojo对象 ,也可以使用@RequestParam 获取单个paramter
- * @param mode
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/login" , params = "method=login" , method = RequestMethod.GET)
- public String login( @ModelAttribute ( "person" ) Person person, Model mode, HttpServletResponse response) throws IOException {
- response.setContentType("text/html;charset=utf-8" );
- Person per = service.getPerson(person.getName());
- if (per != null && per.getName().equals(person.getName()) && per.getPass().equals(person.getPass())) {
- response.getWriter().println(per.getName() + " 你好!" );
- } else {
- response.getWriter().println("登陆失败" );
- }
- response.flushBuffer();
- return null ;
- }
- /**
- * 获取Person详细信息 http://localhost:8080/Web/person/person1 模拟
- *
- * @param name
- * @param mode
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/{name}" , method = RequestMethod.GET)
- public String getPerson( @PathVariable String name, Model mode, HttpServletResponse response) throws IOException {
- response.setContentType("text/html;charset=utf-8" );
- Person per = service.getPerson(name);
- if (per != null ) {
- response.getWriter().println("用户:" + per.getName());
- response.getWriter().println("用户密码:" + per.getPass());
- } else {
- response.getWriter().println("没找到用户" );
- }
- return null ;
- }
- /**
- * 进入新增Person页面 http://localhost:8080/Web/person/addPerson
- *
- * @param mode
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/addPerson" , method = RequestMethod.GET)
- public String initAddPerson(HttpServletResponse response) throws IOException {
- response.setContentType("text/html;charset=utf-8" );
- response.getWriter().println("进入新增页面" );
- return null ;
- }
- /**
- * 新增Person http://localhost:8080/Web/person/addPerson?method=add&name=person11&pass=1
- *
- * @param mode
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/addPerson" , params = "method=add" , method = RequestMethod.GET)
- public String addPerson( @ModelAttribute ( "person" ) Person person, HttpServletResponse response) throws IOException {
- response.setContentType("text/html;charset=utf-8" );
- service.addPerson(person);
- response.getWriter().println("新增成功" );
- response.getWriter().println("用户:" + person.getName());
- response.getWriter().println("用户密码:" + person.getPass());
- response.flushBuffer();
- return null ;
- }
- /**
- * 删除Perseon
- * http://localhost:8080/Web/person/person1/delete
- * @param mode
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/{name}/delete" , method = RequestMethod.GET)
- public String deletePerson( @PathVariable ( "name" ) String name,Model mode, HttpServletResponse response) throws IOException {
- response.setContentType("text/html;charset=utf-8" );
- service.deletePerson(name);
- response.getWriter().println("删除成功" + name);
- response.flushBuffer();
- return null ;
- }
- /**
- * mapTest
- * http://localhost:8080/Web/person/maptest
- * @param mode
- * @return
- * @throws IOException
- */
- @RequestMapping (value = "/maptest" , method = RequestMethod.GET)
- @ResponseBody
- public Map<String, Object> mapTest(HttpServletResponse response) throws IOException {
- System.out.println("HHHHHHHHHH" );
- /* response.setContentType("text/html;charset=utf-8"); */
- Person per = new Person();
- per.setName("xiogjin" );
- per.setPass("123456" );
- List list = new ArrayList();
- list.add("1" );
- list.add("2" );
- list.add("3" );
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("str" , "xiongjin" );
- map.put("object" , per);
- map.put("object" , per);
- map.put("list" , list);
- return map;
- /**
- * 下面的例子将打印{"str":"xiongjin","list":["1","2","3"],"object":{"name":"xiogjin","pass":"123456"}}
- * 通过如下方式可以读取
- * var ob = {"str":"xiongjin","list":["1","2","3"],"object":{"name":"xiogjin","pass":"123456"}};
- alert(ob.str);
- alert(ob.list)
- alert(ob.list[0])
- alert(ob.list[1])
- alert(ob.list[2])
- alert(ob.object.name)
- alert(ob.object.pass)
- */
- }
- }
- class Person {
- private String name;
- private String pass;
- public Person() {
- super ();
- }
- public Person(String name, String pass) {
- super ();
- this .name = name;
- this .pass = pass;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this .name = name;
- }
- public String getPass() {
- return pass;
- }
- public void setPass(String pass) {
- this .pass = pass;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", pass=" + pass + "]" ;
- }
- }
- class PersonService {
- static Map<String, Person> persons = new HashMap<String, Person>();
- public PersonService() {
- for ( int i = 0 ; i < 10 ; i++) {
- persons.put("person" + i, new Person( "person" + i, "" + i));
- }
- }
- public Person getPerson(String name) {
- return persons.get(name);
- }
- public List<Person> getPersons(String name) {
- return new ArrayList<Person>(persons.values());
- }
- public void addPerson(Person per) {
- persons.put(per.getName(), per);
- }
- public void deletePerson(String name) {
- persons.remove(name);
- }
- }
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: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_2_5.xsd" id= "WebApp_ID" version= "2.5" >
- <display-name>SpringMVC</display-name>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
- <load-on-startup>1 </load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
spring-servlent.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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <bean id="viewResolver"
- class = "org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/jsp/" p:suffix= ".jsp" />
- <context:component-scan base-package = "com.benx" />
- <bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- //支持json
- <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
- <property name="messageConverters" >
- <list>
- <bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" ></bean>
- </list>
- </property>
- </bean>
- </beans>