实验报告5-SpringMVC实现页面
一、需求分析
使用Spring MVC框架,从视图、控制器和模型三方面实验动态页面。模拟实现用户登录,模拟的用户名密码以模型属性方式存放在Spring容器中,控制器相应用户请求并映射参数,页面收集用户数据或者展示控制器返回信息。
二、编码实现
1、新建项目
项目:maven,Archetype:maven-archetype-webapp,GroupID:com.sw
引入pom依赖
2、补充项目目录
src.main目录下,新建Java功能目录,resources功能目录,
java包下,新建com.sw.controller包、com.sw.service包、com.sw.dao包、com.sw.pojo包
3、配置Spring
resources目录,applicationContext.xml
<!-- 配置 Spring MVC 要扫描的包 -->
<context:component-scan base-package="com.sw"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
WEB-INF目录下,新建pages目录
4、配置Servlet
web.xml文件
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,读取Spring MVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5、数据封装类
com.sw.pojo包,User类
@Data
@Component
public class User {
private int id;
@Value("zhangsan")
private String username;
@Value("123456")
private String password;
}
6、dao层
com.sw.dao包,UserDao接口
public interface UserDao {
boolean login(User loginUser);
}
com.sw.dao.impl包,UserDaoImpl实现类
@Repository
public class UserDaoImpl implements UserDao {
@Resource(name = "user")
private User dbUser;
@Override
public boolean login(User loginUser) {
if (loginUser.getUsername().equals(dbUser.getUsername())&&
loginUser.getPassword().equals(dbUser.getPassword())){
return true;
}
return false;
}
}
7、service层
com.sw.service包,UserService接口
public interface UserService {
boolean login(User loginUser);
}
com.sw.service.impl包,UserServiceImpl实现类
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public boolean login(User loginUser) {
return userDao.login(loginUser);
}
}
8、controller层
com.sw.controller包,UserController类
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/index")
public String index(){
return "user/index";
}
@GetMapping("/login")
public String login(){
return "user/login";
}
@PostMapping("/login")
public String login(User loginUser){
boolean login = userService.login(loginUser);
if (login){
return "user/index";
}
return "user/login";
}
}
9、jsp页面
WEB-INF/pages目录,新建user/index.jsp
<body>
<h2>用户首页</h2>
</body>
WEB-INF/pages目录,新建user/login.jsp
<body>
<h2>用户登录页</h2>
<form action="/user/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" ></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
10、Add Configurations
Add New Configuration→Tomcat Server→Local
Server:
Name:Tomcat8
Application server:配置本机的Tomcat
Deployment:
Add→Artifact...→项目名:war
Application context:/项目名 或者 /