springmvc-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动生成两个bean,而且做了很多事 ,两个handleMapping寻找映射-->
<mvc:annotation-driven/>
<!-- 扫包 -->
<context:component-scan base-package="cn.bdqn.controller"></context:component-scan>
<!-- 业务逻辑视图去找物理视图,配置路径 ,叫做jsp视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/js/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 class:DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<!-- 配置参数 -->
<param-name>contextConfigLocation</param-name>
<!-- 默认去找web-inf 下的springmvc-servlet.xml,所以需要更改 -->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<!-- 获取springmvc下的所有请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
package cn.bdqn.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndV
@Controller
public class HelloController{
//extends AbstractController{
//protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
//HttpServletResponse arg1) throws Exception {
//ModelAndView mv=new ModelAndView();
//mv.setViewName(“hello”);
//return mv;
//}
@RequestMapping(value=”/hello.html” ,method=RequestMethod.GET )
/**
* value=”parma2” 传过来的参数名,required=true 必须有值 ,defaultValue=”“默认空字符串
* @param parm1
* @return
* @throws Exception
*/
public ModelAndView hello(@RequestParam(value=”param2” ,required=true ,defaultValue=”“) String param1) throws Exception{
System.out.println(“参数是”+param1);
ModelAndView mv=new ModelAndView();
mv.setViewName(“hello”);///WEB-INF/js/hello.jsp
mv.addObject(“msg”, “这是一条信息”);
return mv;
}
@RequestMapping(“/hello2.html”)
public String method2(@RequestParam(defaultValue=”“)int num,
Model model,
HttpServletRequest request,
HttpSession session,
Map map )throws Exception{
map.put(“msg1”, “这是一条消息吗?”);
return “hello”;
}
@RequestMapping(“/hello3.html”)
public String method3(@RequestParam(value=”username”,defaultValue=”“,required=false) String username,
@RequestParam(value=”“,defaultValue=”“,required=false)String password ,
HttpSession session
){
System.out.println(username);
if(username.equals(“root”)&&password.equals(“1111”)){
session.setAttribute(“username”, username);
}
System.out.println(username);
System.out.println(1111);
return “hello”;