L1302spring mvc core applications -1

L130201X1

springmvc achieve supermarket management system user login operation

Transformation implemented in the project on the basis L1301

1, after the new applicationContext-jdbc.xml resource directory under the spring configuration file copied in the head, after the addition of the following code is stored, the scan code is mainly used for the packet to specify the following documents, for the annotation

<context:component-scan base-package="cn.smbms.service"></context:component-scan>

<context:component-scan base-package="cn.smbms.dao"></context:component-scan>

2, open the web.xml, arranged following code

(1) configured to point to the code spring configuration file:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext-*.xml</param-value>

  </context-param>

(2) to configure the listener, search ContextLoaderListener copy its fully qualified name, the listener realizes servletcontextlistener monitor interface, you can start when the web container, the container to initialize the spring, the premise is to go to the configuration in web.xml

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

  </listener>

3, springmvc-servlet.xml code is not changed

4, transformation dao layer

In cn.smbms dao new folder, create user folder, the folder in the user copy UserDao.java and UserDaoImpl.java inside, in front of the class name comment UserDaoImpl.java plus @Repository

5, the service layer transformation

In the new service cn.smbms folder service, establishing user folder, the folder in the user copy UserService.java and UserServiceImpl.java inside, in front of the class name plus UserServiceImpl.java @Service annotations, while Private UserDao userDao; preceded @Resource,

6, create cn.smbms.controller layer

UserController.java code is as follows:

@Controller

@RequestMapping("/user")

public class UserController{

private Logger logger=Logger.getLogger(UserController.class);

@Resource

private UserService userService;

// Skip to realize the landing page

@RequestMapping(value="/login.html")

public String login(){

logger.info("UserController welcome SMBMS============");

return "the Login";} // implement the login

@RequestMapping(value="/dologin.html",method=RequestMethod.POST)

public String dologin(@RequestParam String userCode,

@RequestParam String userPassword) {// call the service method, user matches

User user=userService.login(userCode, userPassword);

IF ( null ! = the User) {// // successful landing page jump (frame.jsp)

return "redirect:/user/main.html";

} The else {// page jump (the login.jsp)

return "login"; } }

@RequestMapping(value="/main.html")

public String main(){

return "frame"; }}

7, a copy of the resource file into the project

(1) copy of the static resource files, calendar, css, images, js

(2) public pages foot.jsp, head.jsp, frame.jsp, login.jsp

(3) Kit: cn.smbms.tools comprising ConfigManager.java, Constants.java, Singleton.java, TestSingleton.java

(4) Copy: BaseDao.java class

(5) copies of the database files and log files log4j.properties database.properties

(6) copy of the database connections required jar package mysql-connector-java-5.1.0-bin.jar

8, the test results: Enter the correct user name and password to enter the frame page, enter dologin error page

9, the configuration in the Welcome page of the web configuration

<welcome-file-list>

   <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>

</welcome-file-list>

Browser to access the web site can be made

http://localhost:8080/L130101X1SpringMVC/user/login.html改为

http://localhost:8080/L130101X1SpringMVC/

L130203X1 Springmvc implement user login page prompts session

1, the following code transformation UserController.java

@Controller

@RequestMapping("/user")

public class UserController{

private Logger logger=Logger.getLogger(UserController.class);

@Resource

private UserService userService;

// Skip to realize the landing page

@RequestMapping(value="/login.html")

public String login(){

logger.info("UserController welcome SMBMS============");

return "login";} // implement the login

@RequestMapping(value="/dologin.html",method=RequestMethod.POST)

public String dologin(@RequestParam String userCode,

@RequestParam String userPassword,

HttpSession session, HttpServletRequest request){

// call the service method, user matches

User user=userService.login(userCode, userPassword);

if (null! = user) {// successful landing

session.setAttribute(Constants.USER_SESSION, user);

// page jump (frame.jsp)

return "redirect:/user/main.html";

}else{

// page jump (login.jsp), with a message

request.setAttribute ( "error", "user name or password is incorrect");

return "login"; }}

@RequestMapping(value="/main.html")

public String main(HttpSession session){

if(session.getAttribute(Constants.USER_SESSION)==null){

return "redirect:/user/login.html";}

return "frame";}}

2, the test results, the login is successful display Good afternoon! The system administrator, you are welcome! Logon failure: display user name or password is incorrect

L130203X2 use springmvc achieve results page mapping

1, the following new WebRoot statics folder, calendar, css, images, js files in which

2, open configuration following code springmvc-servlet.xml

<mvc:resources location="/statics/"

mapping="/statics/**"/>

3, adjust the page style reference path

4, browser access appears style

Local exception handling test L130204X1

1, add the following code in UserController.java

@RequestMapping(value="exlogin.html",method=RequestMethod.GET)

public String exLogin(@RequestParam String userCode,

@RequestParam String userPassword){

logger.info("exLogin =========");

User user=userService.login(userCode, userPassword);

if(user==null){

the throw new new a RuntimeException ( "user name or password is incorrect");

return "redirect:/user/main.html";}

@ExceptionHandler(value={RuntimeException.class})

public String handlerException(RuntimeException e,

HttpServletRequest req){

req.setAttribute("e", e);

return "error"; }

2, the error.jsp new jsp directory, which code for the

${e.message }

3, browser, enter the URL access

http://localhost:8080/L130101X1SpringMVC/user/exlogin.html?userCode=admin&userPassword=123

4, the results of browser output: user name or password is incorrect

L130204X2 test all of exception handling

1, the local variable configuration code commented UserController.java

@ExceptionHandler(value={RuntimeException.class})

public String handlerException(RuntimeException e,

2, springmvc-servlet.xml into the global variable to configure

<bean

class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<property name="exceptionMappings">

<props>

<prop key="java.lang.RuntimeException">error</prop>

</props>

</property>

</bean>

3, the code was changed to error.jsp

${exception.message }

4, browser access

http://localhost:8080/L130101X1SpringMVC/user/exlogin.html?userCode=admin&userPassword=123

Result: The user name or password is incorrect

L130205X1 user queries to achieve transformation

SSJ--->springmvc,springjdbc

1, add the following code in UserController.java

@RequestMapping(value="/userlist.html")

public String gerUserList(Model model,

@RequestParam(value="queryname",required=false) String queryUserName,

@RequestParam(value="queryUserRole",required=false) String queryUserRole,

@RequestParam(value="pageIndex",required=false) String pageIndex ){

logger.info("getUserList ---- > queryUserName: " + queryUserName);

logger.info("getUserList ---- > queryUserRole: " + queryUserRole);

logger.info("getUserList ---- > pageIndex: " + pageIndex);

int _queryUserRole = 0;

List<User> userList = null;

int the pageSize = the Constants. the pageSize ; // set page capacity

int currentPageNo =. 1; // current page

if(queryUserName == null){

queryUserName = "";}

if(queryUserRole != null && !queryUserRole.equals("")){

_queryUserRole = Integer.parseInt(queryUserRole);}

if(pageIndex != null){

try{currentPageNo = Integer.valueOf(pageIndex);

}catch(NumberFormatException e){

return "redirect:/user/syserror.html"; }   }

int The totalCount = userService.getUserCount (queryUserName, _queryUserRole); // total number (Table)

PageSupport pages=new PageSupport();

pages.setCurrentPageNo(currentPageNo);

pages.setPageSize(pageSize);

pages.setTotalCount(totalCount);

int total page count = pages.getTotalPageCount (); //总页数

IF (currentPageNo <. 1) {// First and Last control

currentPageNo = 1;

}else if(currentPageNo > totalPageCount){

currentPageNo = totalPageCount;   }

userList = userService.getUserList(queryUserName,_queryUserRole,currentPageNo,pageSize);

model.addAttribute("userList", userList);

List<Role> roleList = null;

roleplayers list roleService.getRoleList = ();

model.addAttribute ( "roleList", roleList)

model.addAttribute("queryUserName", queryUserName);

model.addAttribute("queryUserRole", queryUserRole);

model.addAttribute ( "totalPageCount", totalPageCount)

model.addAttribute("totalCount", totalCount);

model.addAttribute("currentPageNo", currentPageNo);

return "userlist";}

@RequestMapping(value="/syserror.html")

public String sysError(){

return "syserror"; }

2, a common class of related projects into the copy

(1) cn.smbms.dao role under the new directory, copy RoleDao.java, RoleDaoImpl.java

(2) cn.smbms.pojo the copy Role.java

(3) cn.smbms.service role under the new directory, copy RoleService.java, RoleServiceImpl.java

(4) jsp below copy userlist.jsp, rollpage.jsp, while the modified reference path in the page

Guess you like

Origin www.cnblogs.com/pere/p/11827551.html