二十一、MVC的WEB框架(Spring MVC)

一、基于注解方式配置

1、首先是修改IndexContoller控制器类

  1.1、在类前面加上@Controller:表示这个类是一个控制器

  1.2、在方法handleRequest前面加上@RequestMapping("/index"):表示路径/index会用射到该方法

  1.3、不能让IndexController在实现Controller接口

IndexControlller.java具体实现如下:

package com.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequsetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController{
@RequestMapping("/index")
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception{
ModelAndView mav=new ModelAndView("index");
mav.addObject("name","I love you");
return mav;
}
}

2、修改配置文件springmvc-servlet.xml

由于我们使用了注解方式实现,所以把之前的配置可以去掉,增加如下代码:

<!--表示从包controller下扫描所有注解@Controller的类-->
<context component-scan base-package="controller"/>

具体配置如下:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--扫描包下面有注解@Controller的所有的类-->
<context:component-scan base-package="controller" />
<bean id="irViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<!--注释掉之前基于xml方式的bean的配置-->
<!-- <bean id="simpleUrlHandlerMapping" -->
<!-- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> -->
<!-- <property name="mappings"> -->
<!-- <props> -->
<!-- <prop key="/index">indexController</prop> -->
<!-- </props> -->
<!-- </property> -->
<!-- </bean> -->
<!-- <bean id="indexController" class="controller.IndexController"></bean> -->
</beans>

二、Session的应用

Session在用户登录,一些特殊场合在页面间传递数据的时候会经常用到;

1、修改IndexController控制器类

为方法提供参数HttpSession session,这样可以在方法中使用session。新增一个check方法。

package com.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequsetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController{
@RequestMapping("/index")
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception{
ModelAndView mav=new ModelAndView("index");
mav.addObject("name","I love you");
return mav;
} @RequestMapping("/check")
publc ModelAndView check(HttpSession session){
Integer i=(Integer)session.getAttribute("count");
if(i==null){
i=0;
}
else{
i++
}
session.setAttribute("count",i);
ModelAndView mav=new ModelAndView("check");
return mav;
}
}

2、新建一个check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isElIgnored="false"%>
session中记录的访问次数:${count}

三、SpringMVC的中文问题

在Spring MVC中处理中文问题和Filter处理中文问题是一样的。都是通过修改web.xml配置文件。

1、修改web.xml配置文件

<!--新增一个filter配置解决中文问题-->
<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>

具体配置web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>

2、jsp页面也要使用相同对应的编码方式

新增一个addProduct.jsp,设置字符集为UTF-8

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" isELIgnored="false"%> <form action="addProduct" method="post"> 产品名称 :<input type="text" name="name" value=""><br />
产品价格: <input type="text" name="price" value=""><br /> <input type="submit" value="增加商品">
</form>

四、上传文件功能解决

修改web.xml配置文件,允许访问*.jpg等文件

1、在web.xml中新增一段

<!--表示允许访问jpg文件-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--因为配置springmvc的servlet使用的路径是"/",导致静态资源无法访问,所以加上这一段,才能允许访问-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
<!--<url-pattern>/*.do</url-pattern>加上这个,可以不用配置,允许所有的 -->
</servlet-mapping>
<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>
</web-app>

2、配置springmvc-servlet.xml

新增一段配置:

<bean id="multipartRevoler" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="controller" />
<bean id="irViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>

3、准备上传文件的jsp

新增一个upload.jsp文件

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" import="java.util.*" isElIgnored="false" %>
<form action="uploadImage" method="post" enctype="multipart/form-data">
选择图片:<input type="file" name="image" accept="image/*"/><br/>
上传文件:<input type="submit" value="上传"/>
</form>

4、准备UploadedImageFile的实体类

package com.demo.pojo;

import org.springframework.web.multipart.MultipartFile;

publc class UploadedImageFile{
MultipartFile image;//封装MultipartImage类型的属性image,用于接受页面的注入
//image与jsp里的input的name属性值保持相同。
public MultipartFile getImage(){
return image;
} public void setImge(MultipartFile image){
this.image=image;
}
}

5、上传控制器UploadController

新建一个控制器类

package com.demo.controller;

import java.io.File;
import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.xwork.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequsetMapping;
import org.springframework.web.servlet.ModelAndView; import com.demo.pojo.UploadedImageFile; @Controller
public class IndexController{
@RequestMapping("/uploadImage")
public ModelAndView upload(HttpServletRequest request,UploadedImageFile file) throws IOException,IllegalStateException{
String name=RandomStringUtils.randomAlphanumeric(10);
String newFileName=name+".jpg";
File newFile=new File(request.getServletContext().getRealPath("/image"),newFileName);
newFile.getParentFile().mkdirs();
file.getImage().transferTo(newFile);
ModelAndView mav=new ModelAndView("showUploadedFile");
mav.addObject("imageName",newFileName);
return mav;
} }

6、准备showUploaddFile.jsp显示图片的页面

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" import="java.util.*" isElIgnored="false" %>
<img src="data:image/${imageName}"/>
上一篇:Python 列表生成式、生成器、迭代器


下一篇:【370】Python列表生成式(for 写入一行)