springmvc项目入门通用步骤

springmvc项目入门通用步骤

此处是单纯的springmvc,并没有涉及与数据库打交道和mybatis

1.导入jar包
2.在web.xml中配置springmvc的前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
  	id="WebApp_ID" version="3.1">
  	<!-- 配置加载Spring文件的监听器-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
		     org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>
		     org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
	<!-- 配置Spring MVC前端核心控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>
		     org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>
		<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
    	<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!--/:拦截所有请求(除了jsp)-->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

这里还包括了拦截器和编码过滤

3.在src下创建springmvc的配置文件springmvc-config.xml,配置组件扫描器和视图解析器
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 配置包扫描器,扫描@Controller注解的类 -->
	<context:component-scan base-package="controller" />
	<!-- 加载注解驱动 -->
	<mvc:annotation-driven />
	
	<!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
	<mvc:resources location="/js/" mapping="/js/**" />
	<!-- <mvc:default-servlet-handler /> -->
	<!-- 配置视图解析器 -->
	<bean class=
    "org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

4.在src下创建po包并创建customer持久类
package po;


public class Customer {

	private Integer id;
	private String username;
	private String jobs;
	private String phone;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getJobs() {
		return jobs;
	}
	public void setJobs(String jobs) {
		this.jobs = jobs;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
	}
	
	
}

5.在src下创建controller包并创建CustomerContoller类
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import po.Result;
import po.Customer;

@Controller
public class UserController {
	
	@RequestMapping("/testJson")
	@ResponseBody
	public String testJson(@RequestBody User user) {
		System.out.println(user);
		Result result = new Result(0,"获取成功",user);
		return result.toJSON();
	}
	
	
	@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
	@ResponseBody
	public String selectUser(@PathVariable("id") String id) {
		System.out.println(id);
		User user = new User();
		if(id.equals("1234")) {
			user.setUsername("tom");
		}
		Result result = new Result(0,"查询成功",user);
		return result.toJSON();
	}

}

你可能感兴趣的:(java,spring,mvc,java)