由上一篇配置好后,可以在beans.xml文件下补充
<context:component-scan base-package="cn.tedu.web"/>
<mvc:annotation-driven/>
src/main/java下新建包cn.tedu.web
包下建立DemoControler.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DemoControler {
@RequestMapping("/login.do")
@ResponseBody
public String login(String username,String password) {
if("Tom".equals(username)&&"123".equals(password)) {
return "登录成功";
}else {
return "登录失败";
}
}
}
webapp下新建login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录</h1>
<form action="login.do">
<div><label>用户</label><input type="text" name="user"></div>
<div><label>密码</label><input type="password" name="password"></div>
<div><input type="submit" value="登录"></div>
</form>
</body>
</html>
然后run as一下访问http://localhost:8080/springmvc3/login.html
即可