parent
47b40e3a48
commit
bea7611840
@ -0,0 +1,36 @@ |
|||||||
|
package spring.demo.control; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Controller; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
|
||||||
|
import spring.demo.service.DemoService; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @Description: Controller |
||||||
|
* @author: Bing Yue |
||||||
|
*/ |
||||||
|
@Controller |
||||||
|
public class DemoController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private DemoService demoService; |
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping("login") |
||||||
|
private ModelAndView login(@RequestParam(value = "username", required = false)String username, |
||||||
|
@RequestParam(value = "password", required = false)String password){ |
||||||
|
String result=demoService.Verify(username,password); |
||||||
|
ModelAndView mv=new ModelAndView(); |
||||||
|
//添加模型,可以是任意的POJO对象
|
||||||
|
mv.addObject("validation",result); |
||||||
|
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
|
||||||
|
//即cityAir.jsp
|
||||||
|
mv.setViewName("result"); |
||||||
|
return mv; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
package spring.demo.service; |
||||||
|
|
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
@Service |
||||||
|
public class DemoService { |
||||||
|
|
||||||
|
public String Verify(String username,String password){ |
||||||
|
|
||||||
|
if(username.equals("admin") && password.equals("admin")){ |
||||||
|
return "Login Success!"; |
||||||
|
}else{ |
||||||
|
return "Login Failed,Please Try Again!"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
<?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:p="http://www.springframework.org/schema/p" |
||||||
|
xmlns:tx="http://www.springframework.org/schema/tx" |
||||||
|
xmlns:context="http://www.springframework.org/schema/context" |
||||||
|
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/context |
||||||
|
http://www.springframework.org/schema/context/spring-context-3.0.xsd |
||||||
|
"> |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans> |
@ -0,0 +1,46 @@ |
|||||||
|
<?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: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.xsd |
||||||
|
http://www.springframework.org/schema/mvc |
||||||
|
http://www.springframework.org/schema/mvc/spring-mvc.xsd |
||||||
|
http://www.springframework.org/schema/context |
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd" |
||||||
|
default-lazy-init="true"> |
||||||
|
|
||||||
|
<!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 --> |
||||||
|
<!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 --> |
||||||
|
<mvc:resources location="/*.html" mapping="/**.html" /> |
||||||
|
<mvc:resources location="/css/*" mapping="/css/**" /> |
||||||
|
<mvc:resources location="/js/*" mapping="/js/**" /> |
||||||
|
<mvc:resources location="/images/*" mapping="/images/**" /> |
||||||
|
|
||||||
|
<!-- 启用spring mvc 注解 --> |
||||||
|
<context:annotation-config /> |
||||||
|
<!-- 添加注解驱动 --> |
||||||
|
<mvc:annotation-driven /> |
||||||
|
<!-- 默认扫描的包路径 --> |
||||||
|
<context:component-scan base-package="spring.demo" /> |
||||||
|
|
||||||
|
<!-- 完成请求和注解POJO的映射 --> |
||||||
|
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> |
||||||
|
|
||||||
|
<!-- 处理器映射 HandlerMapping --> |
||||||
|
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> |
||||||
|
|
||||||
|
<!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View --> |
||||||
|
<!-- 像下面这样设置,如果请求为/index,则不通过controller,而直接解析为/index.jsp --> |
||||||
|
<mvc:view-controller path="/index" view-name="index" /> |
||||||
|
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> |
||||||
|
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> |
||||||
|
<!-- 配置jsp路径前缀 --> |
||||||
|
<property name="prefix" value="/"></property> |
||||||
|
<!-- 配置URl后缀 --> |
||||||
|
<property name="suffix" value=".jsp"></property> |
||||||
|
</bean> |
||||||
|
|
||||||
|
</beans> |
@ -0,0 +1,48 @@ |
|||||||
|
<?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" |
||||||
|
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" |
||||||
|
version="3.0"> |
||||||
|
<display-name>easy-springmvc-maven</display-name> |
||||||
|
|
||||||
|
<!-- 上下文监听 --> |
||||||
|
<listener> |
||||||
|
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
||||||
|
</listener> |
||||||
|
|
||||||
|
<!-- spring MVC的核心就是DispatcherServlet,使用springMVC的第一步就是将下面的servlet放入web.xml |
||||||
|
servlet-name属性非常重要,默认情况下,DispatchServlet会加载这个名字-servlet.xml的文件,如下,就会加载 |
||||||
|
dispather-servlet.xml,也是在WEN-INF目录下。 |
||||||
|
--> |
||||||
|
<servlet> |
||||||
|
<servlet-name>dispatcher</servlet-name> |
||||||
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
||||||
|
<load-on-startup>1</load-on-startup> |
||||||
|
</servlet> |
||||||
|
<!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 --> |
||||||
|
<servlet-mapping> |
||||||
|
<servlet-name>dispatcher</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> |
||||||
|
|
||||||
|
|
||||||
|
</web-app> |
@ -0,0 +1,17 @@ |
|||||||
|
<%@ page language="java" contentType="text/html; charset=UTF-8" |
||||||
|
pageEncoding="UTF-8"%> |
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||||||
|
|
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
||||||
|
<title>Login Page!</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<form action="login"> |
||||||
|
用户:<input id="username" name="username" type="text"></input><br> |
||||||
|
密码:<input id="username" name="password" type="password"></input><br> |
||||||
|
<input type="submit" value="登录"> |
||||||
|
</form> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,11 @@ |
|||||||
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
||||||
|
<title>Result Page!</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
${validation } |
||||||
|
</body> |
||||||
|
</html> |
Loading…
Reference in new issue