关闭

springmvc环境整合

标签: javaspringspring mvc框架
239人阅读 评论(0) 收藏 举报
分类:
spring mvc + spring + mybatis 整合


1、创建后台配置文件springmvc-back.xml(单独在web.xml中配置)


jsp页面放在web-inf下面,这样不能直接访问,比较安全!


在web-inf下建立文件back_page用于存放后台页面


springmvc-back.xml中配置视图解析器:前缀为back_page文件夹中的文件


名,后缀为.jsp


    <!-- 配置扫包 -->
       <context:component-scan base-package="cn.zy" use-default-


filters="false">
     <context:include-filter type="annotation" 


expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>




注意!这里为 <context:include-filter
     
     <!-- jsp视图解析器 -->
  	<bean id="jspViewResolver"
		


class="org.springframework.web.servlet.view.InternalResourceViewR


esolver">
		<property name="prefix" value="/WEB-


INF/back_page/" />
		<property name="suffix" value=".jsp" />
	</bean>






2、同上,创建前台配置文件springmvc-front.xml


    <!-- 配置扫包 -->
       <context:component-scan base-package="cn.zy" use-default-


filters="false">
     <context:include-filter type="annotation" 


expression="org.springframework.stereotype.Controller"/>
     </context:com ponent-scan>


     <!-- jsp视图解析器 -->
  	<bean id="jspViewResolver"
		


class="org.springframework.web.servlet.view.InternalResourceViewR


esolver">
		<property name="prefix" value="/WEB-


INF/front_page/" />
		<property name="suffix" value=".jsp" />
	</bean>




3、配置web.xml(配置上述的前台和后台的配置文件,springmvc配置


servlet)


<!-- springmvc 后台配置 -->
	<servlet>
		<servlet-name>back</servlet-name>
		<servlet-


class>org.springframework.web.servlet.DispatcherServlet</servlet-


class>
		<init-param>
			<param-


name>contextConfigLocation</param-name>
			<param-value>classpath:spring-


back.xml</param-value>
		</init-param>
	</servlet>
    <servlet-mapping>
    <servlet-name>back</servlet-name>
    <!-- 过滤.do -->
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    	<!-- springmvc 前台配置 -->
	<servlet>
		<servlet-name>front</servlet-name>
		<servlet-


class>org.springframework.web.servlet.DispatcherServlet</servlet-


class>
		<init-param>
			<param-


name>contextConfigLocation</param-name>
			<param-value>classpath:spring-


front.xml</param-value>
		</init-param>
	</servlet>
    <servlet-mapping>
    <servlet-name>front</servlet-name>
    <!-- 过滤.shtml,前台请求 -->
    <url-pattern>*.shtml</url-pattern>
    </servlet-mapping>



4、过滤乱码


<filter>
		<filter-name>characterEncodingFilter</filter-


name>
		<filter-


class>org.springframework.web.filter.CharacterEncodingFilter</fil


ter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-


name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>




---------------------------------------------------------------
测试是否配置成功:


cn.zy.core.controller下新建UserInfoController


WEB-INF下新建jsp页面userinfo_add.jsp


@Controller
public class UserInfoController {


	//配置前端页面对应请求路径
	@RequestMapping("user/add.do")
	public String add(UserInfo user){
		
		System.out.println("----------"+user);
		//跳转到对应页面
		return "index";
	}
	
}





<%@ 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>Insert title here</title>
</head>
<body>
<form action="user/add.do" method="post">
姓名:<input type="text" name="userName" /><br/>
性别:<input type="text" name="userSex"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19392次
    • 积分:462
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    最新评论