spring mvc 3.1.1

spring mvc 3.1.1    (spring-framework-3.1.1.RELEASE -all)


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.base.test.entity.User;

@Controller
public class IndexController {

	@RequestMapping("/index")
    public String index() {
		System.out.println("in index");
        return "index";
    }
	

	// 直接跳转到页面,可用get
	@RequestMapping(value = "/tologin", method = RequestMethod.GET)
    public String login() {
        return "login";
    }
   
	
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login2(HttpServletRequest request) {
        String userName = request.getParameter("userName").trim();
        request.setAttribute("userName", userName); // 传参数到页面
        System.out.println(userName);
        if("admin".equals(userName))
        	return "loginSuccess";
        return "loginError";
    }
    
    @RequestMapping(value = "login2", method = RequestMethod.POST)
    public String testParam(User user, Map<String, Object> model) {
    	model.put("userName",user.getUserName()); // 传参数到页面 ${userName} 即可
    	return "loginSuccess";
    }

//---------------------------------------------
	@RequestMapping("/test/login2")
	public ModelAndView testLogin2( String userName, String password, String age){
		// request和response不必非要出现在方法中,如果用不上的话可以去掉
		// 参数的名称是与页面控件的name相匹配,参数类型会自动被转换
		System.out.println(userName+"," + password+","+age);
		if (!"admin".equals(userName) || !"admin".equals(password) ) {
			return new ModelAndView("loginError"); // 手动实例化ModelAndView完成跳转页面(转发),效果等同于上面的方法返回字符串
		}
		return new ModelAndView(new RedirectView("../index.jsp"));  // 采用重定向方式跳转页面
		// 重定向还有一种简单写法
		// return new ModelAndView("redirect:../index.jsp");
	}
	
	@RequestMapping("/test/login3")
	public ModelAndView testLogin3(User user) {
		// 同样支持参数为表单对象,类似于Struts的ActionForm,User不需要任何配置,直接写即可
		String username = user.getUserName();
		String password = user.getPassword();
		System.out.println(username+","+password);
		if (!"admin".equals(username) || !"admin".equals(password) ) {
			return new ModelAndView("loginError");
		}
		return new ModelAndView("redirect:../index.htm");  // 跳转到另一个action
	}
}

 

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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-3.0.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
   	<!-- 启用spring mvc 注解 -->
	<context:annotation-config />

   <!-- 完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

   
    <!-- 使Spring支持自动检测组件,如注解的Controller  -->
    <context:component-scan base-package="com.base.test.springmvc.controller"/>
   
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
          
    <mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/index.htm" />
			<bean class="com.base.test.springmvc.controller.MyInteceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

 	<!-- 对静态资源文件的访问  -->
  
	<mvc:default-servlet-handler/>

</beans>

 

 

<?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">

	<!-- 为Listener指定spring配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring-cfg/applicationContext-*.xml
		</param-value>
	</context-param>
	<!-- 服务器启动时,实例化Spring容器 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<!-- 为了spring中scope=request,session -->
	<listener>
		<listener-class>
			org.springframework.web.context.request.RequestContextListener
		</listener-class>
	</listener>





    <servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
 <!-- 拦截/*,这是一个错误的方式,请求可以走到Action中,但转到jsp时再次被拦截,不能访问到jsp。 -->

 <servlet-mapping>
  
	<servlet-name>springmvc</servlet-name>
 
	<url-pattern>/</url-pattern>
 
</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值