一.SpringMVC概述


package com.po;
import java.io.Serializable;
public class Users implements Serializable {
private String uname;// 用户名
private String passwd;// 密码
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
然后在com.biz包中新建IUsersBiz接口和实现类,代码如下:
package com.biz;
import com.po.Users;
public interface IUsersBiz {
/**
* 检查用户
* @param us
* @return
*/
public boolean check(Users us);
}
package com.biz;
import org.springframework.stereotype.Service;
import com.po.Users;
@Service("UsersBiz")
public class UsersBiz implements IUsersBiz {
/**
* 检查用户名和密码是否合法,合法返回true,否则返回false。
*/
@Override
public boolean check(Users us) {
if (us != null) {
if (us.getUname() != null || us.getUname().trim().equals("")
&& us.getPasswd() != null
|| us.getPasswd().trim().equals("")) {
return true;
}
}
return false;
}
}
再然后在com.action中新建UsersAction类,代码如下:
package com.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.biz.IUsersBiz;
import com.po.Users;
@Controller
public class UsersAction {
@Resource(name="UsersBiz")
private IUsersBiz iuBiz;
public IUsersBiz getIuBiz() {
return iuBiz;
}
public void setIuBiz(IUsersBiz iuBiz) {
this.iuBiz = iuBiz;
}
/******SpringMvc的执行方法*******/
@RequestMapping(value="check_Users.do")
public ModelAndView check(HttpServletRequest request, HttpServletResponse response, Users us) {
ModelAndView mv = new ModelAndView();
boolean flag = iuBiz.check(us);
if(flag) {
//将us放入request作用域中
request.setAttribute("us", us);
//跳转页面
mv.setViewName("loginok.jsp");
} else {
mv.setViewName("fail.jsp");
}
return mv;
}
}
代码中加黑的:@RequestMapping作说明:
@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性,下面我们把她分成三类进行说明。
1、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
ModelAndView表示:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
default-autowire="byName"
>
<!-- 织入注解通知配置 -->
<context:annotation-config></context:annotation-config>
<!-- 注入要扫描的组件包 -->
<context:component-scan base-package="com.biz"></context:component-scan>
<context:component-scan base-package="com.action"></context:component-scan>
</beans>
第五步web.xml配置:
关键配置DispatcherServlet、CharacterEncodingFilter
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>prjSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Spring的启动配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>GBK</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>GBK</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>springmvc</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
第六步:login.jsp\loginok.jsp\fail.jsp
1.login.jsp
<body>
用户登录<br>
<form action="check_Users.do" method="post">
账号:<input type="text" name="uname"/><br>
密码:<input type="password" name="passwd"/><br>
<input type="submit" value="确定"/>
</form>
</body>
2.loginok.jsp
<body>
用户名:${us.uname }<br/>
密码:${us.passwd }
</body>
3.fail.jsp
<body>
登录失败 <br>
</body>
好啦看看效果:
最后说明一下,这个例子虽然很简单,但是其中的springmvc配置很重要!!!!