刚刚毕业在公司用的是ssh,但是看网上很多人说spring mvc + mybatis 现在比较好,所以就自己来学习下。看网上的很多实例度有很多配置什么的。我觉得给我这种太新手的新手还是一下子搞不懂。所以就自己看书很文档慢慢的来学习。顺便写个博客,方便以后学习的人,希望能帮到他们。
首先我们需要建立一个web project 然后导入所需要的包。我的项目后面能下载的,因为我要和mybatis结合的所以包就一起放进去了。
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">
<!-- 在别的地方加载配置文件 当然现在还没有别的xml配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/*.xml</param-value>
</context-param>
<!-- spring mvc 处理中文乱码问题 -->
<filter>
<filter-name>Character Encoding</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>Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springmvc的入口 servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>加载classpath下/config/spring/目录下的所有*spring*.xml作为Spring MVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/*spring*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<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>
让后在src下面建立目录如下
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: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支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package="cn.lzg.action"/>
<!-- 配置视图的处理,就是你Controller返回的值,在这里加一个前缀和后缀,就组成了一个 xxx.jsp 找不到这个jsp 就报错了 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
登录的控制器
LoginControlle.java
package cn.lzg.action;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping(value = "login") //相当于struts2 的<action name="login" ...>
public String login(String username,String password,HttpServletRequest request) {
if("啊!@".equals(username) && "123".equals(password)){//帐号和密码都匹配(先写死,等和mybatis结合了在从数据库取)名字写成这样是检测乱码问题的,
//就返回登录成功的页面。为了防止重复提交所以试用redirect方式。
request.getSession().setAttribute("username", username);
System.out.println(username);
return "redirect:/toLogin";
}
else{ //登录失败,返回首页
return "redirect:/index";
}
}
@RequestMapping(value="toLogin")
public String loginSuccess(){
return "success";
}
@RequestMapping(value="index")
public String toIndex(){
return "index"; //和springmvc-servlet.xml 里面配置的视图解析器 合成为 /WEB-INF/jsp/index.jsp 所以就到了首页
}
}
User的实体 (这个例子中没用到,我传值也没用User)
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login" method="post">
<table cellpadding="5" cellspacing="5" border="0" align="center" width="20%">
<thead ><td align="center"><font size="50">用户登录</font></td></thead>
<tr>
<td align="center">
<span>用户名:</span><input type="text" name="username" width="120px;"/>
</td>
</tr>
<tr>
<td align="center">
<span>密 码:</span><input type="text" name="password" width="100%"/>
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="登录"/>
</td>
</tr>
</table>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1 align="center"><font color="red">${username }</font>,欢迎您!</h1>
</body>
</html>
好啦,一个简单的spring mvc 登录的小小demo
在浏览器输入 http://localhost:8080/springmvcdemo/index 就能访问了, 默认用户名 “啊!@” 密码 123 用户名是问了检测中文乱码的问题。
开始在我 前台——>后台 不乱码 但是 后台——>前台乱码 虽然可以用response对象去设置,但是太麻烦我就直接将所有的html问价都加上
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
反正新建一个 jsp 都是复制的吧。没人会总是真的新建把。
菜鸟中的新人发帖,欢迎大家指出不足好错误。或者指导下我应该怎么去学习。谢谢各位大神啦。
demo工程下载地址
http://download.youkuaiyun.com/detail/hunanlzg/7623083