标签:发送post请求 resource property 字符串比较 log load java 耦合 charset
目录
1.创建web工程
2.导入springMVC相关jar包,在spring的基础上添加一个mvc的包
classmate.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.fileupload-1.2.0.jar
com.springsource.org.apache.commons.io-1.4.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
druid-1.0.15.jar
hamcrest-core-1.3.jar
hibernate-validator-5.jar
hibernate-validator-annotation-processor-5.jar
jackson-annotations-2.9.4.jar
jackson-core-2.9.4.jar
jackson-databind-2.9.4.jar
jboss-logging-3.1.1.jar
jstl.jar
junit-4.12.jar
lombok.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-5.0.7.RELEASE.jar
spring-aspects-5.0.7.RELEASE.jar
spring-beans-5.0.7.RELEASE.jar
spring-context-5.0.7.RELEASE.jar
spring-core-5.0.7.RELEASE.jar
spring-expression-5.0.7.RELEASE.jar
spring-jdbc-5.0.7.RELEASE.jar
spring-test-5.0.7.RELEASE.jar
spring-tx-5.0.7.RELEASE.jar
spring-web-5.0.7.RELEASE.jar
spring-webmvc-5.0.7.RELEASE.jar
standard.jar
validation-api-1.1.0.jar
3.添加配置文件springmvc.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
4.配置前端控制器
在web.xml当中配置前端控制器
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>mySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mySpringMVC</servlet-name>
<!-- 设置所有以action结尾的请求进入SpringMVC -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
**springMVC中url-patten取值**
/*
拦截所有 jsp js png .css 真的全拦截.不建议使用
*.action *.do
拦截以do action 结尾的请求
/
拦截所有,不包括jsp,包含.js .png.css 建议使用
5.创建控制器
package com.le.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping("/first.action")
public ModelAndView show(){
ModelAndView modelAndView = new ModelAndView();
//请求过来后, 跳转到别一个界面 往别一个界面当中传一些数据
modelAndView.addObject("name","le");
modelAndView.setViewName("le");
return modelAndView;
}
}
6.在springmvc核心配置文件当中添加控制器扫描范围
<!--注解扫描,扫描指定包下注解@Controller @Service -->
<context:component-scan base-package="com.le"/>
7.创建first.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>first.jsp</h1>
转发过来的数据name=${name}
</body>
</html>
8.index.jsp页面跳转
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/first.action">发送请求-first.jsp</a>
</body>
</html>
DispatcherServlet
HandlerMapping
Handler
HandlAdapter
ViewResolver
View
视图解析器使用SpringMVC框架默认的InternalResourceViewResolver
这个视图解析器支持JSP视图解析
配置视图解析器
? 当一个页面存放目录结构比较多时,就可以配置目录
? springmvc.xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/myxq/"/>
<property name="suffix" value=".jsp"/>
</bean>
? 可以在返回地址上添加前缀和后缀
@Controller
public class MyController {
@RequestMapping("/first.action")
public ModelAndView show(){
ModelAndView modelAndView = new ModelAndView();
//请求过来后, 跳转到别一个界面
//往别一个界面当中传一些数据
modelAndView.addObject("name","le");
modelAndView.setViewName("/first"); //=> /WEB-INF/myxq/first.jsp
return modelAndView;
}
}
web.xml
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>mySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<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>mySpringMVC</servlet-name>
<!-- 拦截所有,不包括jsp,包含.js .png.css 建议使用 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
index.jsp
<a href="${pageContext.request.contextPath}/first.action">发送请求-result.jsp</a>
<a href="${pageContext.request.contextPath}/second.action">发送请求-second.jsp</a>
Controller.java
@Controller
public class MyController {
//匹配请求可以去省略action
@RequestMapping("/first")
public ModelAndView show(){
ModelAndView modelAndView = new ModelAndView();
//请求过来后, 跳转到别一个界面 往别一个界面当中传一些数据
modelAndView.addObject("name","le");
modelAndView.setViewName("/result.jsp");//发送请求不可以去省略action
return modelAndView;
}
@RequestMapping("/second")
public String show2(){
return "/result.jsp";
}
}
@Controller
public class MyController {
//匹配请求可以去省略action
@RequestMapping("/first")
public ModelAndView show(){
ModelAndView modelAndView = new ModelAndView();
//请求过来后, 跳转到别一个界面 往别一个界面当中传一些数据
modelAndView.addObject("name","le");
modelAndView.setViewName("redirect:/second.action");//发送请求不可以去省略action
return modelAndView;
}
@RequestMapping("/second")
public String show2(){
return "redirect:/result.jsp";
}
}
index.jsp
<form action="${pageContext.request.contextPath}/myform">
user:<input type="text" name="user_name"><br>
age:<input type="text" name="user_age" ><br>
<input type="submit" value="提交">
</form>
FormController类
@Controller
public class FormController {
@RequestMapping("myform")
public ModelAndView form(HttpServletRequest request){
String user = request.getParameter("user_name");
String age = request.getParameter("user_age");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.addObject("age",age);
modelAndView.setViewName("/result.jsp");
return modelAndView;
}
}
result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
用户:${user}<br/>
年龄:${age}
</body>
</html>
@Controller
public class FormController {
@RequestMapping("myform")
public ModelAndView form(String user,String age){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.addObject("age",age);
modelAndView.setViewName("/result.jsp");
return modelAndView;
}
}
形参的类型与绑定的类型不一致时,可以使用@RequestParam进行匹配
value
请求参数名字
required
是否必须
默认是true
表示请求中一定要有相应的参数,否则将报错
defaultValue
默认值
表示如果请求中没有同名参数时的默认值
@RequestParam(value = "id",required = true,defaultValue = "100") Integer idkey
参数必须,有默认值时,哪怕请求中没有对应的参数,也不会报错
FormController类
@Controller
public class FormController {
@RequestMapping("myform")
public ModelAndView form(@RequestParam(value="user_name") String user,@RequestParam(value="user_age") String age){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.addObject("age",age);
modelAndView.setViewName("/result.jsp");
return modelAndView;
}
}
User类
package com.le.domain;
import lombok.Getter;
import lombok.Setter;
import java.util.Arrays;
import java.util.List;
@Setter@Getter
public class User {
private String user_name;
private String user_age;
private String hobby[];
private Dog dog;
private List<Dog> dogs;
@Override
public String toString() {
return "User{" +
"user_name='" + user_name + '\'' +
", user_age='" + user_age + '\'' +
", hobby=" + Arrays.toString(hobby) +
", dog=" + dog +
", dogs=" + dogs +
'}';
}
}
表单
<form action="${pageContext.request.contextPath}/myform.action">
user:<input type="text" name="user_name"><br>
age:<input type="text" name="user_age" ><br>
爱好:<input type="checkbox" name="hobby" value="篮球"> 篮球
<input type="checkbox" name="hobby" value="乒乓球"> 乒乓球
<input type="checkbox" name="hobby" value="足球"> 足球
<input type="submit" value="提交">
</form>
表单
<form action="${pageContext.request.contextPath}/myform2">
user:<input type="text" name="user_name"><br>
age:<input type="text" name="user_age" ><br>
爱好:<input type="checkbox" name="hobby" value="篮球"> 篮球
<input type="checkbox" name="hobby" value="乒乓球"> 乒乓球
<input type="checkbox" name="hobby" value="足球"> 足球
<input type="submit" value="提交">
</form>
User类
public class User {
private String user_name;
private String user_age;
private String hobby[];
}
**QueryUser类**
@Setter@Getter
public class QueryUser {
User user;
}
**表单**
<form action="${pageContext.request.contextPath}/myform2">
user:<input type="text" name="user.user_name"><br>
age:<input type="text" name="user.user_age" ><br>
爱好:<input type="checkbox" name="user.hobby" value="篮球"> 篮球
<input type="checkbox" name="user.hobby" value="乒乓球"> 乒乓球
<input type="checkbox" name="user.hobby" value="足球"> 足球
<input type="submit" value="提交">
</form>
**FormController类**
@RequestMapping("myform2")
public ModelAndView form(QueryUser user){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("/result.jsp");
return modelAndView;
}
result.jsp
用户:${user.user_name}<br/>
年龄:${user.user_age}
**User类**
@Setter@Getter
public class User {
private String user_name;
private String user_age;
private String hobby[];
private Dog dog;
private List<Dog> dogs;
}
**表单**
<form action="${pageContext.request.contextPath}/myform2">
user:<input type="text" name="user_name"><br>
age:<input type="text" name="user_age" ><br>
爱好:<input type="checkbox" name="hobby" value="篮球"> 篮球
<input type="checkbox" name="hobby" value="乒乓球"> 乒乓球
<input type="checkbox" name="hobby" value="足球"> 足球
<br>--------宠物---------<br>
宠物名称:<input type="text" name="dog.name"> <br>
宠物颜色:<input type="text" name="dog.color"> <br>
<br>--------宠物---------<br>
宠物名称:<input type="text" name="dogs[0].name"> <br>
宠物颜色:<input type="text" name="dogs[0].color"> <br>
<br>--------宠物---------<br>
宠物名称:<input type="text" name="dogs[1].name"> <br>
宠物颜色:<input type="text" name="dogs[1].color"> <br>
<input type="submit" value="提交">
</form>
@RequestMapping("myform2")
public String myform4(QueryUser user){
return "/result.jsp";
}
?
把发布时间对象的字符串转换成日期接收
@Setter@Getter
public class User {
private String user_name;
private String user_age;
private String hobby[];
private Date birthdate;
}
1.创建转换器
package com.le.web.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
if(s != null){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
2.在springMVC核心配置文件当中自定义转换器
<bean id="dateConverter" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.le.web.converter.DateConverter"></bean>
</list>
</property>
</bean>
<!--注解驱动-->
<mvc:annotation-driven conversion-service="dateConverter"/>
当发送post请求时,带有中文的参数会发生乱码
在web.xml当中添加一个过滤器
<!-- 解决post乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置编码参是UTF8 -->
<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>/*</url-pattern>
</filter-mapping>
标签:发送post请求 resource property 字符串比较 log load java 耦合 charset
原文地址:https://www.cnblogs.com/mumuyinxin/p/10921750.html