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

Springmvc第一讲学习笔记,数据接收与乱码解决

2017-06-24 18:32 489 查看
地址栏没变是转发 return "/index.jsp" 相当于 req.getRequestDispatcher("index.jsp").forword(req,resp);

重定向 resp.sendRedirect("/index.jsp");地址栏是index.jsp 重定向发生改变,相当于重新发送一个请求,地址栏变化

springmvc中的视图解析器

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>

</bean>
核心: 有 视图解析器, 
转发:输入hello1.do, 转发请求hello.do,转到WEB-INF/jsp/hello.jsp

注意:Controller中没有hello.do的映射处理,根据hello1请求的结果,匹配"前缀+return结果+后缀"

仍然能跳转至WEB-INF/jsp/hello.jsp。

但是,如果直接发送url请求为 hello.do,no mapper 。。。。 404错误

@RequestMapping("/hello1")
public String hello1(){
System.out.println("hello1 run ");
return "hello";//内部转发,相当于mv.setViewName("hello");

//成功根据视图解析器的前缀与后缀 转发到WEB-INF/jsp/hello.jsp

}
重定向 有视图解析器

重定向:输入hello1.do, 地址栏变成hello.do.

注意:重定向相当于在地址栏中重新创建一个请求发送。结果是跳转到hello.do的执行页面WEB-INFjsphello.jsp
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
mv.setViewName(hello);
mv.addObject(msg, hello spring mvc);
return mv;
}
@RequestMapping("/hello1")
public String hello1(){

return "redirect:hello.do"; 重定向 hello.do,转到WEB-INF/jsp/hello.jsp
      //return "redirect:hello.jsp"; 默认是转到根目录下的hello.jsp而不是
      //return "redirect:hello"
;外部重定向,相当于重新请求一个叫hello的请求,被web.xml拦截器的 .do 拦截
}
}
数据处理:接收前台的数据
1.spring mvc 是单例的,启动一次服务器后只创建一个Controller,而struts2每次发送action请求都会重新创建Action对象。

2.数据提交,
(1)提交的域名称(url中写的)和映射处理的参数一致即可。 

url: localhost:8080/data/hello.do?name=zhangsan
处理方法
@RequestMapping("/hello")
public String name(String name){
System.out.println(name);
}
(2)提交的是一个对象,要求提交的表单域与对象的属性名一致即可。

url:http://localhost:8080/05result_mvc/hello3.do?name=zhangsan&pwd=1234
@RequestMapping("/hello3")
public ModelAndView name(User user){ 
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");
System.out.println(user);控制台:User [id=0, name=zhangsan, pwd=1234]
return mv;
}
数据处理:将数据显示到表示层
(1)需要ModelAndView,spring配置中需要视图解析器
url:http://localhost:8080/05result_mvc/hello3.do?name=zhangsan&pwd=123

处理映射的类方法

@RequestMapping("/hello3")
public ModelAndView name(User user){ 
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");

//相当于req.setAttribute(name,user.getName());

mv.addObject(name, user.getName());//注意:一定为mv设置addObject,否则在页面也请求不到

//相当于req.setAttribute(pwd,user.getPwd());
mv.addObject(pwd, user.getPwd());
System.out.println(user);
return mv;
}
前台
body
this is hello.jsp <br>
姓名${name }
   <br>
密码${pwd}
<br>

body 
显示
this is hello.jsp 
姓名zhangsan 
密码123 

(2)无视图解析器,使用modelMap(我不怎么喜欢)

url:http://localhost:8080/05result_mvc/hello4.do?name=zhangsan&pwd=123
@RequestMapping("/hello4")
public String noResolver(User user,ModelMap model){
System.out.println(user);
相当于req.setAttribute(name,user.getName());
model.addAttribute(name, user.getName());
model.addAttribute(pwd, user.getPwd());
return "/hello.jsp";转发到根目录的hello.jsp,不是web-inf下的哦
}
ModelAndView 和 ModelMap的区别
相同点:它们都能把数据封装并显示在前台
不同点:ModelAndView可以通过视图解析器指定跳转的页面,而ModelMap需要手动转发,比如return "hello.jsp";
ModelAndView需要视图解析器,而ModelMap不需要
ModelAndView是在方法体内创建,而ModelMap是在方法的参数上。

解决中文乱码:
get乱码,则需要改tomcat的server.xml配置文件,如下:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

改为:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>

post乱码,在项目中的web.xml中配置过滤器:

这里需要注意的是,最好把这段代码放在web.xml中开头的位置,因为拦截有顺序,如果放在后面的话容易拦截不到。

<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: