springMVC配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- springMVC入口 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<!-- url分发器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 找springMVC主配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 控制器类 -->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动TOMCAT容器时就加载DispatcherServlet -->
<load-on-startup>1</load-on-startup>
<!-- 所有的请求都走SpringMVC -->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</servlet>
<!-- 所有的请求都走SpringMVC -->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- IOC注解支持 -->
<context:component-scan base-package="controller" />
<!-- MVC注解支持 -->
<mvc:annotation-driven />
<!-- 配置内部视图解析器 -->
<!-- 视图路径:/WEB-INF/jsp/*.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 静态资源解析 -->
<mvc:resources location="/static/" mapping="/static/**" />
<!-- 全局异常处理 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.RuntimeException">error</prop>
</props>
</property>
</bean>
</beans>
controller类
LoginController.java
/**
* 实现登录功能
*
* @param username
* @param password
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
// @RequestParam(required = true参数必须 说明表单里必须有name属性且值和参数名一样;false参数不必须,value属性值和表单里的name属性值相同) 参数名和表单name属性值相同value属性可以省略
//注解作用:标注参数是必须的
String login(
@RequestParam(required = false, value = "uname") String username,
@RequestParam(required = false, value = "upwd") String password) {
// 防止出现空指针异常
if ("admin".equals(username) && "123456".equals(password)) {
return "index";
}
return "login/login";
}
//等效于
@RequestMapping(value = "/login", method = RequestMethod.POST, params = {
"username", "password" })
String login(String username, String password) {
System.out.println(username);
System.out.println(password);
// 防止出现空指针异常
if ("admin".equals(username) && "123456".equals(password)) {
return "index";
}
return "login/login";
}
UserController.java
@RequestMapping(value = "/useradd", method = RequestMethod.POST)
// 使用对象作参数 ,user的属性名必须和表单的name属性值相同 ,user里面没有的属性在写在后面
String useradd(User user, String reUserPassword) {
System.out.println(user.getUserCode());
System.out.println(user.getUserName());
System.out.println(reUserPassword);
return "user/useradd";
}
把参数传给视图
@RequestMapping(value = "/login", method = RequestMethod.POST, params = {
"username", "password" })
//返回
String login(String username, String password, Model model) {
User user = new User();
if ("admin".equals(username) && "123456".equals(password)) {
user.setUserName(username);
user.setUserPassword(password);
} else {
return "login/login";
}
// 绑定数据
// mav.addAllObjects();//添加多个对象
model.addAttribute(user);
return "index";
}
转发的请求是POST那转发去的页码也必须是POST
@RequestMapping(value = "/login", method=RequestMethod.POST)
String login(){
//返回的是url
return "forward:index.html";
}
@RequestMapping(value = "/index.html", method=RequestMethod.POST)
String index(){
return "index";
}
/* ../上一层 / 根目录 */
重定向 客户端请求两次
String login(){
//返回的是url
return "redirect:index.html";
}