您的位置:首页 > 编程语言 > Java开发

Spring MVC配置

2016-02-18 22:58 471 查看
1,添加相关jar包



2,配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<!-- 过滤器,将浏览器的POST请求转化为PUT/DELETE -->
<filter>
<filter-name>hf</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hf</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置springDispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置文件的路径和名称;也可以不配置,则默认名称为:上面servlet-name值后加"-servlet.xml"默认位置:WEB-INF文件夹下 -->
<!--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
-->
<!-- 启动时自动加载 -->
<load-on-startup>1</load-on-startup>
</servlet>

<!-- 配置请求路径 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>


3,配置springmvc-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> 
<!-- 扫描器 -->
<context:component-scan base-package="com.hanqi"></context:component-scan>

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

</beans>


4,前台页面,发送请求 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>主页</title>
</head>
<body>

<a href="hanqi/hello">发送请求</a>

<form action="hanqi/hello" method="post">

<input type="submit" value="提交POST请求">
</form>
<!-- REST风格 put/delete动作加隐藏域实现 -->
<form action="hanqi/helloDelete/123" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="提交DELETE请求">
</form>

<!-- 属性自动装入对象,并支持级联 -->
<form action="hanqi/hellouser" method="post">
用户ID:<input type="text" name="id"><br>
用户名称:<input type="text" name="username"><br>

部门ID:<input type="text" name="bumen.bumenid"><br>
部门名称:<input type="text" name="bumen.bumenmc">

<input type="submit" value="提交user信息">
</form>
<br>

<div>
<a href="hanqi/helloP?id=123&name=hh">发送带参数的请求</a>
</div>

<div>
<a href="hanqi/helloP?id=123">发送带选参数的请求</a>
</div>

<div>
<a href="hanqi/helloS/123">发送带占位符参数的请求</a>
</div>

</body>
</html>


5,创建控制器类 HelloWorld.java

package com.hanqi;

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

//请求处理类 控制器类
@Controller
@RequestMapping("/hanqi")
public class HelloWorld {

@RequestMapping(value="/hello")//前台请求路径
public String sayHello()
{
System.out.println("hello!world!");

return "success";//返回的页面
}

@RequestMapping(value="/helloP")
public String sayHelloP(@RequestParam("id") int id, @RequestParam(value="name",required=false,defaultValue="ttt") String username)
{
System.out.println("hello!参数:id="+id+"name="+username);

return "success";
}

//接收Spring servlet自动装配好的对象
@RequestMapping(value="/hellouser", method=RequestMethod.POST)
public String sayHellouser(User user)
{
System.out.println("hello!参数:id="+user);

return "success";
}

@RequestMapping(value="/helloS/{id}", method=RequestMethod.POST)
public String sayHelloS(@PathVariable("id") int id)
{
System.out.println("hello!参数:id="+id);

return "success";
}

//占位符 REST风格
@RequestMapping(value="/helloDelete/{id}", method=RequestMethod.DELETE)
public String sayHelloD(@PathVariable("id") int id)
{
System.out.println("hello!delete参数:id="+id);

return "success";
}

@RequestMapping(value="/hello", method=RequestMethod.POST)
public String sayHelloPost()
{
System.out.println("hello!POST");

return "success";
}

}


6,处理返回信息的页面 success.jsp 所在目录即springmvc-servlet.xml视图解析器前缀,习惯为/WEB-INF/views/,只有服务器内部程序可以访问,安全性高.

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>成功</title>
</head>
<body>
返回成功!
</body>
</html>


7,其他:用于演示的实体类 User.java Bumen.java

package com.hanqi;

public class User {

private int id;
private String username;
private Bumen bumen;

@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", bumen=" + bumen + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Bumen getBumen() {
return bumen;
}
public void setBumen(Bumen bumen) {
this.bumen = bumen;
}

}


package com.hanqi;

public class Bumen {

private int bumenid;
private String bumenmc;

@Override
public String toString() {
return "Bumen [bumenid=" + bumenid + ", bumenmc=" + bumenmc + "]";
}
public int getBumenid() {
return bumenid;
}
public void setBumenid(int bumenid) {
this.bumenid = bumenid;
}
public String getBumenmc() {
return bumenmc;
}
public void setBumenmc(String bumenmc) {
this.bumenmc = bumenmc;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: